Sorting algorithms/Heapsort: Difference between revisions
Content added Content deleted
imported>SerErris (→{{header|Groovy}}: added a better to read and commented version of the code.) |
imported>SerErris (→{{header|Groovy}}: changed the loops to even more readable for loops.) |
||
Line 2,958: | Line 2,958: | ||
// ensure that each parent is lager than the child for all parent/childs. |
// ensure that each parent is lager than the child for all parent/childs. |
||
// (list.size() -2) / 2 = last parent in the heap. |
// (list.size() -2) / 2 = last parent in the heap. |
||
((list.size() |
for (start in ((list.size()-2).intdiv(2))..0 ) { |
||
siftDown(list, start, list.size() - 1) |
siftDown(list, start, list.size() - 1) |
||
} |
} |
||
Line 2,968: | Line 2,968: | ||
//Run the list backwards and |
//Run the list backwards and |
||
//for end = (size of list -1 ) to 0 |
//for end = (size of list -1 ) to 0 |
||
((list.size()-1)..0) |
for (end in (list.size()-1)..0 ) { |
||
makeSwap(list, 0, end) //put the top of the heap to the end (largest element) |
makeSwap(list, 0, end) //put the top of the heap to the end (largest element) |
||
siftDown(list, 0, end-1) //ensure that the rest is a heap again |
siftDown(list, 0, end-1) //ensure that the rest is a heap again |