Improve company productivity with a Business Account.Sign Up

x
?
Solved

binary sort

Posted on 2000-04-20
8
Medium Priority
?
287 Views
Last Modified: 2012-05-04
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
Comment
Question by:Francois_P
8 Comments
 
LVL 2

Expert Comment

by:Sage020999
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

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

Author Comment

by:Francois_P
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Francois_P
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

by:BarryTice
ID: 2734960
Francois_P
There is a tutorial at http://www.cs.usask.ca/homepages/undergrads/mmt410/javaproj/tutorial/SortMain.html

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

by:
Tchalkov earned 150 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

by:Francois_P
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

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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

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

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

Join & Ask a Question