Sorting algorithms/Heapsort: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: changed HEIGHT in the PRE html tag.) |
(solved for maxscript) |
||
Line 1,492: | Line 1,492: | ||
1 2 3 4 5 6</lang> |
1 2 3 4 5 6</lang> |
||
=={{header|MAXScript}}== |
|||
<lang MAXScript>fn heapify arr count = |
|||
( |
|||
local s = (count - 1)/2 |
|||
while s > 0 do |
|||
( |
|||
arr = siftDown arr s count |
|||
s -= 1 |
|||
) |
|||
return arr |
|||
) |
|||
fn siftDown arr s end = |
|||
( |
|||
local root = s |
|||
while root * 2 <= end do |
|||
( |
|||
local child = root * 2 |
|||
if child < end and arr[child] < arr[child+1] do |
|||
( |
|||
child += 1 |
|||
) |
|||
if arr[root] < arr[child] then |
|||
( |
|||
swap arr[root] arr[child] |
|||
root = child |
|||
) |
|||
else return arr |
|||
) |
|||
return arr |
|||
) |
|||
fn heapSort arr = |
|||
( |
|||
local count = arr.count |
|||
arr = heapify arr count |
|||
local end = count |
|||
while end >= 1 do |
|||
( |
|||
swap arr[1] arr[end] |
|||
end -= 1 |
|||
arr = siftDown arr 1 end |
|||
) |
|||
)</lang> |
|||
Output: |
|||
<lang MAXScript> |
|||
a = for i in 1 to 10 collect random 0 9 |
|||
#(7, 2, 5, 6, 1, 5, 4, 0, 1, 6) |
|||
heapSort a |
|||
#(0, 1, 1, 2, 4, 5, 5, 6, 6, 7) |
|||
</lang> |
|||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |