Solved

binary sort

Posted on 2000-04-20
8
277 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel object stays open 19 73
Updates not working for MS Windows 7 12 164
Access Object Property from VBA Module in Excel 2010 2 31
Excel Automation VBA 19 69
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…
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 …
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…
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…

776 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