Sorting algorithms/Heapsort: Difference between revisions

Line 1,994:
 
=={{header|Julia}}==
<lang julia>function percolate_downpd!(a, first, last)
r = first
while (c = 2 * r - 1) < last
Line 2,009:
end
hfy!(a, n) = (f = div(n, 2); while f >= 1 pd!(a, f, n); f -= 1 end)
function heapify!(a, n)
first = div(n, 2)
while first >= 1
percolate_down!(a, first, n)
first -= 1
end
end
 
function heapsort!(a)
n = length(a)
heapify!(a, n)
last = n
while last > 1
a[1], a[last] = a[last], a[1]
last -= 1
percolate_down!(a, 1, last)
end
a
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)
a = shuffle(collect(1:12))
println("Unsorted: $a")
4,107

edits