Solved

binary sort

Posted on 2000-04-20
275 Views
can someone give me the binary sort algo, I need this one to sort combobox which is fill with alpha-numeric item.
There is many item in the combobox so I need a rapid sort. If the binary sort isn't the fastest way to sort it please tell me how else can I do it, thank!
0
Question by:Francois_P

LVL 2

Expert Comment

ID: 2734897
' ************************************************
' Quicksort with:
'   - Uses Rnd to select a random dividing value
'   - Stops when there are fewer than CutOff items
'       left to sort. It then finishes using
'       SelectionSort.
' ************************************************
Public Sub Quicksort(List() As Long, ByVal min As Long, ByVal max As Long)
Dim med_value As Long
Dim hi As Long
Dim lo As Long
Dim i As Long

' If the list has no more than CutOff elements,
' finish it off with SelectionSort.
If max - min < CutOff Then
Selectionsort List(), min, max
Exit Sub
End If

' Pick the dividing value.
i = Int((max - min + 1) * Rnd + min)
med_value = List(i)

' Swap it to the front.
List(i) = List(min)

lo = min
hi = max
Do
' Look down from hi for a value < med_value.
Do While List(hi) >= med_value
hi = hi - 1
If hi <= lo Then Exit Do
Loop
If hi <= lo Then
List(lo) = med_value
Exit Do
End If

' Swap the lo and hi values.
List(lo) = List(hi)

' Look up from lo for a value >= med_value.
lo = lo + 1
Do While List(lo) < med_value
lo = lo + 1
If lo >= hi Then Exit Do
Loop
If lo >= hi Then
lo = hi
List(hi) = med_value
Exit Do
End If

' Swap the lo and hi values.
List(hi) = List(lo)
Loop

' Sort the two sublists.
Quicksort List(), min, lo - 1
Quicksort List(), lo + 1, max
End Sub
0

LVL 2

Expert Comment

ID: 2734934
if you set the property 'Sorted' to true
the combo will always be sorted after inserting items
0

Author Comment

ID: 2734945
That not exactly what I was searching for, I was looking for a way to sort a combobox with alpha-numeric content by using a recursive sort, by exemple the binary sort or any other sort faster on many items!

Thank for giving me an answer but that not exactly what I was looking for, thank anyway.
0

Author Comment

ID: 2734954
I know that it will be sort but not in a logical order
it will give me something like:

1
11
111
2334
A
A222
Z33
ZZZ

When I'm looking for a result more like:

1
A
11
111
Z33
ZZZ
2334
A222
0

LVL 7

Expert Comment

ID: 2734960
Francois_P

It gives explanations of the binary sort and the quick sort (and the bubble sort, though that would likely be too slow for your needs), with an interactive demo stepping through the process.

Hope this helps. -- b.r.t.
0

LVL 2

Accepted Solution

Tchalkov earned 50 total points
ID: 2735004
then try this:

Private Function Smaller(str1 As String, str2 As String) As Boolean
If (Len(str1) < Len(str2)) Then
Smaller = True
Exit Function
End If
If (Len(str1) > Len(str2)) Then
Smaller = False
Exit Function
End If
If (str1 < str2) Then
Smaller = True
Exit Function
End If
Smaller = False
End Function
Private Sub Swap(cbo As ComboBox, i As Integer, j As Integer)
Dim str As String
str = cbo.List(i)
cbo.List(i) = cbo.List(j)
cbo.List(j) = str
End Sub
Private Sub QSort(cbo As ComboBox, L As Integer, R As Integer)
Dim i As Integer
Dim j As Integer
Dim m As String
m = cbo.List((L + R) / 2)
i = L
j = R
While (i <= j)
While (Smaller(cbo.List(i), m) And i <= j)
i = i + 1
Wend
While (Smaller(m, cbo.List(j)) And i <= j)
j = j - 1
Wend
If (i <= j) Then
Swap cbo, i, j
i = i + 1
j = j - 1
End If
Wend
If (i < R) Then QSort cbo, i, R
If (j > L) Then QSort cbo, L, j
End Sub

you call this way:
QSort YourCombo, 0, YourCombo.ListCount - 1
0

Author Comment

ID: 2737587
This is almost what I was looking for but this is still a little bit too slow for what I have to do the combobox will contain 1000+ item minimum so I've got to sort his order rapidly. If someone can help me find a simillar way but faster a little bit faster then this one it would be very appreciated
0

Author Comment

ID: 2747581
I hav emodified the code you gaved me to fit my needs, this was very helpfull thank.
0

Join & Write a Comment Already a member? Login.

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!