Sorting algorithms/Quicksort: Difference between revisions
Content added Content deleted
m (→{{header|AppleScript}}: →Straightforward: Tidied.) |
|||
Line 896: | Line 896: | ||
===Straightforward=== |
===Straightforward=== |
||
Emphasising clarity, |
Emphasising clarity, quick sorting, ''and'' correct AppleScript: |
||
<lang applescript> |
<lang applescript>-- In-place Quicksort (basic algorithm). |
||
Algorithm: S.A.R. (Tony) Hoare, 1960. |
-- Algorithm: S.A.R. (Tony) Hoare, 1960. |
||
⚫ | |||
*) |
|||
⚫ | |||
if (listLength < 2) then return |
|||
-- Sort range l thru r of a list, in place. |
|||
⚫ | |||
⚫ | |||
⚫ | |||
script o -- Script object containing both the target list and the recursive process. |
|||
⚫ | |||
⚫ | |||
⚫ | |||
-- Script object containing the list as a property (to allow faster references to its items) |
|||
-- and the recursive subhandler. |
|||
script o |
|||
property lst : theList |
property lst : theList |
||
Line 912: | Line 918: | ||
set j to r |
set j to r |
||
repeat until (i > j) |
repeat until (i > j) |
||
set |
set lv to my lst's item i |
||
repeat while ( |
repeat while (pivot > lv) |
||
set i to i + 1 |
set i to i + 1 |
||
set |
set lv to my lst's item i |
||
end repeat |
end repeat |
||
set |
set rv to my lst's item j |
||
repeat while ( |
repeat while (rv > pivot) |
||
set j to j - 1 |
set j to j - 1 |
||
set |
set rv to my lst's item j |
||
end repeat |
end repeat |
||
if (j > i) then |
if (j > i) then |
||
set my lst's item i to |
set my lst's item i to rv |
||
set my lst's item j to |
set my lst's item j to lv |
||
else if (i > j) then |
else if (i > j) then |
||
exit repeat |
exit repeat |
||
Line 940: | Line 946: | ||
end script |
end script |
||
tell o to qsrt(l, r) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
-- Do the sort. |
|||
tell o to qsrt(l, r) |
|||
end if |
|||
return -- nothing |
return -- nothing. |
||
end quicksort |
end quicksort |
||
property sort : quicksort |
property sort : quicksort |
||
-- |
-- Demo: |
||
local aList |
|||
set aList to {28, 9, 95, 22, 67, 55, 20, 41, 60, 53, 100, 72, 19, 67, 14, 42, 29, 20, 74, 39} |
set aList to {28, 9, 95, 22, 67, 55, 20, 41, 60, 53, 100, 72, 19, 67, 14, 42, 29, 20, 74, 39} |
||
sort(aList, 1, -1) -- Sort |
sort(aList, 1, -1) -- Sort items 1 thru -1 of aList. |
||
return aList</lang> |
return aList</lang> |
||
{{output}} |
{{output}} |
||
<lang applescript>{9, 14, 19, 20, 20, 22, 28, 29, 39, 41, 42, 53, 55, 60, 67, 67, 72, 74, 95, 100}</lang> |
<lang applescript>{9, 14, 19, 20, 20, 22, 28, 29, 39, 41, 42, 53, 55, 60, 67, 67, 72, 74, 95, 100}</lang> |
||
=={{header|Arc}}== |
=={{header|Arc}}== |
||
<lang Arc>(def qs (seq) |
<lang Arc>(def qs (seq) |