LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
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
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Quicksort for multi-columns Joakim Norrman[_2_] Excel Programming 4 May 10th 09 09:00 PM
User Function Question: Collect Condition in Dialog Box - But How toInsert into Function Equation? SteveM Excel Programming 1 January 3rd 08 03:45 PM
Can this QuickSort work? RB Smissaert Excel Programming 24 June 27th 06 11:57 AM
Question for Alan on QuickSort Marston Excel Programming 3 August 31st 04 05:20 PM
recursion depth, 'Out of stack space' in Quicksort marcel Excel Programming 0 April 21st 04 09:11 PM


All times are GMT +1. The time now is 02:48 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"