View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
JJ[_13_] JJ[_13_] is offline
external usenet poster
 
Posts: 2
Default Help with my quicksort function

I wrote a quicksort function but it doesn't seem to properly sort the list.
A few of the entry in the list is still not sorted. The list is supposed to
be sort by the number of members - largest first. Below is the code (start
with `Main()`).

'Teams array is a list of team information where each entry contains:
'(1) = Team name (string)
'(2) = Number of members (number)
'(3) up to (22) = Member indexes (number)
Dim Teams(1 To 100, 1 To 22)

Sub CopyTeamEntry(ByRef SrcList, SrcNum, ByRef DestList, DestNum)
For i = 1 To 22
DestList(DestNum, i) = SrcList(SrcNum, i)
Next
End Sub

Sub SortTeams(ByRef List, startIdx, endIdx)
Dim tmp(1 To 1, 1 To 22)
If startIdx < endIdx Then
midIdx = Int((startIdx + endIdx) / 2)
midVal = List(midIdx, 2)
loIdx = startIdx
hiIdx = endIdx
While loIdx < hiIdx
If List(loIdx, 2) midVal Then
loIdx = loIdx + 1
ElseIf List(hiIdx, 2) <= midVal Then
hiIdx = hiIdx - 1
Else
'swap entries
CopyTeamEntry List, loIdx, tmp, 1
CopyTeamEntry List, hiIdx, List, loIdx
CopyTeamEntry tmp, 1, List, hiIdx
End If
Wend
'sort both half
SortTeams List, startIdx, midIdx
SortTeams List, midIdx + 1, endIdx
End If
End Sub

Sub Main()
'populate Teams array...
teamCnt = 0
'some code to populate Teams array...
teamCnt = 80 'just an example for this context

SortTeams Teams, 1, teamCnt
End Sub