Sorting algorithms/Heapsort: Difference between revisions

m
(→‎{{header|Wren}}: Added library method version.)
Line 2,491:
 
=={{header|Julia}}==
<lang julia>swapafunction swap(a, i, j) = begin a[i], a[j] = a[j], a[i] end
a[i], a[j] = a[j], a[i]
 
end
function pd!(a, first, last)
while (c = 2 * first - 1) < last
Line 2,499 ⟶ 2,501:
end
if a[first] < a[c]
swapaswap(a, c, first)
first = c
else
Line 2,506 ⟶ 2,508:
end
end
function heapify!(a, n)
hfy!(a, n) = (f = div(n, 2); while f >= 1 pd!(a, f, n); f -= 1 end)
f = div(n, 2)
 
while f >= 1
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)
pd!(a, f, n)
f -= 1
end
end
function heapsort!(a)
n = length(a)
heapify!(a, n)
l = n
while l > 1
swap(a, 1, l)
l -= 1
pd!(a, 1, l)
end
return a
end
 
using Random: shuffle
a = shuffle(collect(1:12))
println("Unsorted: $a")
Anonymous user