Anonymous user
Sorting algorithms/Heapsort: Difference between revisions
m
→{{header|Julia}}
(→{{header|Wren}}: Added library method version.) |
|||
Line 2,491:
=={{header|Julia}}==
<lang julia>
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]
first = c
else
Line 2,506 ⟶ 2,508:
end
end
function heapify!(a, n)
f = div(n, 2)
while f >= 1
pd!(a, f, n)
f -= 1
end
end
function heapsort!(a)
n = length(a)
heapify!(a, 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")
|