|
|
|
Bubble Sort for Points and Lists
Added on 6/30/1999
|
Here"s a little handler that will sort points by either their locV (default) or their locH (if you use a non-void second parameter).
On my Mac 7600/132, it reverses the order of a 1000 item list in less than 5 ticks, and reverses the order of a 100 item list in less that 1/5th of a tick.
The principle can be adapted to sorting a list by any value (for example, the area of a rect). Have fun.
on bubbleSort pointList, horizontally
-- SORTS VERTICALLY BY DEFAULT
set horizontally = not voidP (horizontally)
set pointCount = count (pointList)
set bubble = TRUE
repeat while bubble
set i = pointCount -1
set bubble = FALSE
repeat while i
if horizontally then
set xLoc1 = the locH of getAt (pointList, i)
set xLoc2 = the locH of getAt (pointList, i + 1)
else
set xLoc1 = the locV of getAt (pointList, i)
set xLoc2 = the locV of getAt (pointList, i + 1)
end if
if xLoc1 > xLoc2 then
set bubble = TRUE
set xLoc1 = getAt (pointList, i)
deleteAt pointList, i
addAt pointList, i + 1, xLoc1
end if
set i = i - 1
end repeat
end repeat
return pointList
end bubbleSort
|
|