Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
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. |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
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. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Quicksort for multi-columns | Excel Programming | |||
User Function Question: Collect Condition in Dialog Box - But How toInsert into Function Equation? | Excel Programming | |||
Can this QuickSort work? | Excel Programming | |||
Question for Alan on QuickSort | Excel Programming | |||
recursion depth, 'Out of stack space' in Quicksort | Excel Programming |