Jump to content

Sorting algorithms/Heapsort: Difference between revisions

m
Line 1,994:
 
=={{header|Julia}}==
<lang julia>function pd!swapa(a, firsti, lastj) = begin a[i], a[j] = a[j], a[i] end
 
function pd!(a, first, last)
while (c = 2 * first - 1) < last
if c < last && a[c] < a[c + 1]
Line 2,000 ⟶ 2,002:
end
if a[first] < a[c]
swapa(a[, c], a[first] = a[first], a[c])
first = c
else
Line 2,010 ⟶ 2,012:
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 swapa(a[, 1], a[l] = a[l], a[1]); l -= 1; pd!(a, 1, l) end; a)
 
a = shuffle(collect(1:12))
4,108

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.