Sorting algorithms/Heapsort: Difference between revisions

Content added Content deleted
m (→‎{{header|Go}}: library change)
No edit summary
Line 1,217: Line 1,217:


1 2 3 4 5 6</lang>
1 2 3 4 5 6</lang>

=={{header|NetRexx}}==
<lang NetRexx>/* NetRexx */
options replace format comments java crossref savelog symbols binary

import java.util.List

placesList = [String -
"UK London", "US New York", "US Boston", "US Washington" -
, "UK Washington", "US Birmingham", "UK Birmingham", "UK Boston" -
]

lists = [ -
placesList -
, heapSort(String[] Arrays.copyOf(placesList, placesList.length)) -
]

loop ln = 0 to lists.length - 1
cl = lists[ln]
loop ct = 0 to cl.length - 1
say cl[ct]
end ct
say
end ln

return

method heapSort(a = String[], count = a.length) public constant binary returns String[]


rl = String[a.length]
al = List heapSort(Arrays.asList(a), count)
al.toArray(rl)

return rl

method heapSort(a = List, count = a.size) public constant binary returns ArrayList

a = heapify(a, count)

iend = count - 1
loop label iend while iend > 0
swap = a.get(0)
a.set(0, a.get(iend))
a.set(iend, swap)
a = siftDown(a, 0, iend - 1)
iend = iend - 1
end iend

return ArrayList(a)

method heapify(a = List, count = int) public constant binary returns List

start = (count - 2) % 2

loop label strt while start >= 0
a = siftDown(a, start, count - 1)
start = start - 1
end strt

return a

method siftDown(a = List, istart = int, iend = int) public constant binary returns List

root = istart

loop label root while root * 2 + 1 <= iend
child = root * 2 + 1
if child + 1 <= iend then do
if (Comparable a.get(child)).compareTo(Comparable a.get(child + 1)) < 0 then do
child = child + 1
end
end
if (Comparable a.get(root)).compareTo(Comparable a.get(child)) < 0 then do
swap = a.get(root)
a.set(root, a.get(child))
a.set(child, swap)
root = child
end
else do
leave root
end
end root

return a
</lang>
;Output
<pre>
UK London
US New York
US Boston
US Washington
UK Washington
US Birmingham
UK Birmingham
UK Boston

UK Birmingham
UK Boston
UK London
UK Washington
US Birmingham
US Boston
US New York
US Washington
</pre>


=={{header|OCaml}}==
=={{header|OCaml}}==