Solved

binary sort

Posted on 2000-04-20
8
276 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

867 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now