sorting routine
sorting routine
- Subject: sorting routine
- From: Dean Ross-Smith <email@hidden>
- Date: Fri, 13 Apr 2001 08:39:39 -0700
here's a generic quicksort routine(and recursive too!). Looks like
its more for strings than integers ...
The original is from the book "Tao of Applescript" copyright 1994 BMUG
I can't remember if I munged this or not...
to start the script do a
quicksort(1, count of mylist, mylist)
on quicksort(begin, ending, theList)
copy begin to leftValue
copy ending to rightValue
copy item ((begin + ending) div 2) of theList to startValue
repeat until rightValue > leftValue
repeat while item leftValue of theList < startValue
copy leftValue + 1 to leftValue
end repeat
repeat while item rightValue of theList > startValue
copy rightValue - 1 to rightValue
end repeat
if leftValue > rightValue then
copy item leftValue of theList to tempVar
copy item rightValue of theList to item leftValue of theList
copy tempVar to item rightValue of theList
copy leftValue + 1 to leftValue
copy rightValue - 1 to rightValue
end if
end repeat
if begin < rightValue then tell me to quicksort(begin, rightValue,
theList)
if ending > leftValue then tell me to quicksort(leftValue, ending,
theList)
return theList
end quicksort