Sorting algorithms/Heapsort: Difference between revisions
Content added Content deleted
m (→{{header|AppleScript}}: Tidied.) |
|||
Line 657: | Line 657: | ||
-- Heap sort algorithm: J.W.J. Williams. |
-- Heap sort algorithm: J.W.J. Williams. |
||
on heapSort(theList, l, r) -- Sort items l thru r of theList. |
on heapSort(theList, l, r) -- Sort items l thru r of theList. |
||
⚫ | |||
⚫ | |||
-- Convert negative and/or transposed range indices. |
|||
⚫ | |||
⚫ | |||
if (l > r) then set {l, r} to {r, l} |
|||
⚫ | |||
script o |
script o |
||
-- The list |
-- The list as a script property to allow faster references to its items. |
||
-- where l is the left index of the sort range (top of heap), n the node index, and c the number of children per node. |
|||
⚫ | |||
⚫ | |||
property lst : theList |
property lst : theList |
||
⚫ | |||
⚫ | |||
⚫ | |||
on hsrt(l, r) |
|||
⚫ | |||
⚫ | |||
set child to node * 2 - const |
|||
siftDown(item i of my lst, i, r) |
|||
⚫ | |||
⚫ | |||
-- Sort the heap. |
|||
⚫ | |||
-- Remove the value at the end. |
|||
⚫ | |||
-- Move the highest value in the heap from the top to the vacated slot. |
|||
set item endOfHeap of my lst to item l of my lst |
|||
-- Sift the removed value back into the reduced heap from the top. |
|||
siftDown(removedValue, l, endOfHeap - 1) |
|||
end repeat |
|||
end hsrt |
|||
⚫ | |||
⚫ | |||
⚫ | |||
set child to root * 2 - const |
|||
repeat until (child comes after endOfHeap) |
repeat until (child comes after endOfHeap) |
||
set childV to my lst's item child |
|||
set childValue to item child of my lst |
|||
if (child comes before endOfHeap) then |
if (child comes before endOfHeap) then |
||
set child2 to child + 1 |
set child2 to child + 1 |
||
set |
set child2V to my lst's item child2 |
||
if ( |
if (child2V > childV) then |
||
set child to child2 |
set child to child2 |
||
set |
set childV to child2V |
||
end if |
end if |
||
end if |
end if |
||
if (childV > siftV) then |
|||
set my lst's item node to childV |
|||
set node to child |
|||
set child to node * 2 - const |
|||
set item root of my lst to childValue |
|||
set root to child |
|||
set child to root * 2 - const |
|||
else |
else |
||
exit repeat |
exit repeat |
||
Line 708: | Line 693: | ||
-- Insert the sifted-down value at the node reached. |
-- Insert the sifted-down value at the node reached. |
||
set |
set my lst's item node to siftV |
||
end siftDown |
end siftDown |
||
end script |
end script |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
tell o to siftDown(its lst's item i, i, r) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
repeat with endOfHeap from r to (l + 1) by -1 |
|||
set endV to o's lst's item endOfHeap |
|||
⚫ | |||
-- Do the sort. |
|||
o |
tell o to siftDown(endV, l, endOfHeap - 1) |
||
end |
end repeat |
||
return -- nothing |
return -- nothing |
||
end heapSort |
end heapSort |
||
property sort : heapSort |
|||
-- Demo: |
|||
set aList to {} |
|||
local aList |
|||
repeat with i from 1 to 25 |
|||
⚫ | |||
set end of aList to (random number 100) |
|||
⚫ | |||
end repeat |
|||
⚫ | |||
⚫ | |||
set sortedCopy to aList's items |
|||
heapSort(sortedCopy, 1, -1) |
|||
set astid to AppleScript's text item delimiters |
|||
set AppleScript's text item delimiters to ", " |
|||
set output to "Original: {" & aList & "} |
|||
Sorted: {" & sortedCopy & "}" |
|||
set AppleScript's text item delimiters to astid |
|||
⚫ | |||
{{output}} |
{{output}} |
||
< |
<lang applescript>9, 10, 17, 20, 27, 32, 33, 36, 44, 51, 55, 56, 60, 60, 62, 62, 65, 72, 74, 76, 85, 86, 86, 87, 95}</lang> |
||
⚫ | |||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |