Sorting algorithms/Heapsort: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
imported>SerErris (→{{header|Groovy}}: changed the loops to even more readable for loops.) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(4 intermediate revisions by 4 users not shown) | |||
Line 2,251:
.
data[] = [ 29 4 72 44 55 26 27 77 92 5 ]
print data[]
</syntaxhighlight>
Line 3,008:
toList (Node x l r) = x : toList (merge l r)
e.g
Line 5,586:
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func sift_down(a, start, end) {
var root = start
while ((2*root + 1) <= end) {
var child = (2*root + 1)
if ((child+1 <= end) && (a[child] < a[child + 1])) {
child += 1
}
if (a[root] < a[child]) {
a[child, root] = a[root, child]
root = child
} else {
return
}
}
}
func heapify(a, count) {
var start = ((count - 2) / 2)
while (start >= 0) {
sift_down(a, start, count-1)
start -= 1
}
}
func heap_sort(a, count) {
heapify(a, count)
var end = (count - 1)
while (end > 0) {
a[0, end] = a[end, 0]
end -= 1
sift_down(a, 0, end)
}
Line 5,620:
}
var arr = (1..10 -> shuffle)
say arr
heap_sort(arr, arr.len)
say arr
{{out}}
<pre>[10, 5, 2, 1, 7, 6, 4, 8, 3, 9]
Line 6,220:
=={{header|Wren}}==
<syntaxhighlight lang="
var root = start
while (root*2 + 1 <= end) {
Line 6,257:
}
var
for (a in
System.print("Before: %(a)")
heapSort.call(a)
Line 6,276:
Alternatively, we can just call a library method.
{{libheader|Wren-sort}}
<syntaxhighlight lang="
var
for (a in
System.print("Before: %(a)")
Sort.heap(a)
|