Sorting algorithms/Heapsort: Difference between revisions

Content added Content deleted
Line 1,994: Line 1,994:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>function pd!(a, first, last)
<lang julia>swapa(a, i, j) = begin a[i], a[j] = a[j], a[i] end

function pd!(a, first, last)
while (c = 2 * first - 1) < last
while (c = 2 * first - 1) < last
if c < last && a[c] < a[c + 1]
if c < last && a[c] < a[c + 1]
Line 2,000: Line 2,002:
end
end
if a[first] < a[c]
if a[first] < a[c]
a[c], a[first] = a[first], a[c]
swapa(a, c, first)
first = c
first = c
else
else
Line 2,010: Line 2,012:
hfy!(a, n) = (f = div(n, 2); while f >= 1 pd!(a, f, n); f -= 1 end)
hfy!(a, n) = (f = div(n, 2); while f >= 1 pd!(a, f, n); f -= 1 end)


heapsort!(a) = (n = length(a); hfy!(a, n); l = n; while l > 1 a[1], a[l] = a[l], a[1]; l -= 1; pd!(a, 1, l) end; a)
heapsort!(a) = (n = length(a); hfy!(a, n); l = n; while l > 1 swapa(a, 1, l); l -= 1; pd!(a, 1, l) end; a)


a = shuffle(collect(1:12))
a = shuffle(collect(1:12))