Sorting algorithms/Quicksort: Difference between revisions

m
Line 896:
===Straightforward===
 
Emphasising clarity, simplicity,quick performancesorting, ''and'' correct AppleScript:
 
<lang applescript>(*-- In-place Quicksort (basic algorithm).
-- Algorithm: S.A.R. (Tony) Hoare, 1960.
on quicksort(theList, l, r) -- Sort items l thru r of theList.
*)
ifset (listLength >to 1)(count thentheList)
 
if (listLength < 2) then return
-- Sort range l thru r of a list, in place.
-- Convert negative and/or transposed range indices.
on quicksort(theList, l, r)
if (l < 0) then set l to listLength + l + 1
script o -- Script object containing both the target list and the recursive process.
if (r < 0) then set r to listLength + r + 1
if (l > r) then set {l, r} to {r, l}
-- Script object containing the list as a property (to allow faster references to its items)
-- and the recursive subhandler.
script o
property lst : theList
Line 912 ⟶ 918:
set j to r
repeat until (i > j)
set leftValuelv to my lst's item i
repeat while (leftValuepivot <> pivotlv)
set i to i + 1
set leftValuelv to my lst's item i
end repeat
set rightValuerv to my lst's item j
repeat while (rightValuerv > pivot)
set j to j - 1
set rightValuerv to my lst's item j
end repeat
if (j > i) then
set my lst's item i to rightValuerv
set my lst's item j to leftValuelv
else if (i > j) then
exit repeat
Line 940 ⟶ 946:
end script
settell listLengtho to qsrt(countl, theListr)
if (listLength > 1) then
-- Convert negative and/or transposed range indices.
if (l < 0) then set l to listLength + l + 1
if (r < 0) then set r to listLength + r + 1
if (l > r) then set {l, r} to {r, l}
-- Do the sort.
tell o to qsrt(l, r)
end if
return -- nothing. The input list has been sorted in place.
end quicksort
property sort : quicksort
 
-- Test codeDemo:
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}
sort(aList, 1, -1) -- Sort theitems entire1 listthru -1 of aList.
return aList</lang>
 
{{output}}
<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}}==
<lang Arc>(def qs (seq)
557

edits