Solved

binary sort

Posted on 2000-04-20
8
278 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

829 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