I guess since we’re on the subject of sorting, I’ll throw a few sorting functions up here, in case anyone needs them. I make no assurances about bugs, etc., they seem to work fine for me but they haven’t been systematically tested so it’s possible edge cases might break them.
You can change the data type of the toSort() array to double, string, etc., or you change it to a class if there’s some property of the objects that can be compared with > and < and =. You just need to change the data type of the temp variable and, if you’re using objects, change the 1 or 2 places where values are compared to refer to the property you want to compare. There’s probably a more generic way to do it, rather than having to overload & tweak the sorting method for each different class/type, but it’s good enough for me (I tend to not like things that are super-genericized).
First up,
[h]QuickSort:[/h]
[code]Sub QuickSort(toSort() as integer, left as integer = -1, right as integer = -1)
if left = -1 and right = -1 then
left = 0
right = toSort.Ubound
end
dim temp as integer //change data type as needed
dim left2, right2, pivot as integer
left2 = left
right2 = right
if (right - left) > 0 then
pivot = (right + left) / 2
while left2 <= pivot and right2 >= pivot
while toSort(left2) < toSort(pivot) and left2 <= pivot
left2 = left2 + 1
wend
while toSort(right2) > toSort(pivot) and right2 >= pivot
right2 = right2 - 1
wend
'swap values
temp = toSort(left2)
toSort(left2) = toSort(right2)
toSort(right2) = temp
left2 = left2 + 1
right2 = right2 - 1
if left2 - 1 = pivot then
right2 = right2 + 1
pivot = right2
Elseif right2 + 1 = pivot then
left2 = left2 - 1
pivot = left2
end
Wend
QuickSort(toSort,left,pivot-1)
QuickSort(toSort,pivot+1,right)
end
End Sub
[/code]