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 |
Help with my quicksort function
On Sun, 29 Oct 2017 22:30:47 +0700, JJ wrote:
'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) The number of members can be vary, BTW. |
Help with my quicksort function
JJ wrote:
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()`). [snip code] http://rosettacode.org/wiki/Sorting_.../Quicksort#VBA No need to reinvent the wheel. -- I'm not sure what it is with little girls and that blood-curdling scream. I wouldn't make a sound like that unless I'd discovered a body. |
All times are GMT +1. The time now is 09:30 AM. |
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com