?
Solved

Binary quick sorting

Posted on 2002-06-12
7
Medium Priority
?
319 Views
Last Modified: 2010-05-02
I am looking for the full algorithm for binary quick sorting in VB.
The algorithm should sort a array of strings in binary order.
Help appreciated...


Martin Schmalz
0
Comment
Question by:cyber_bulldog
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 2

Expert Comment

by:vbDoc
ID: 7073887
0
 
LVL 5

Expert Comment

by:rpai
ID: 7073916
0
 

Expert Comment

by:HobbitHouse
ID: 7074159
[vbcode]
'
' test QSORT on a vector of strings
'
Private Sub cmdSort3_Click()
    Dim thelist(10) As String
    Dim ix As Integer
    For ix = 0 To 9
        thelist(ix) = Chr(70 + ((ix + 5) Mod 9)) + "aB"
    Next ix
    Debug.Print "-----------"
    For ix = 0 To 9
        Debug.Print thelist(ix)
    Next ix
    Call QuickSort(thelist, 0, 9)
    Debug.Print ""
    For ix = 0 To 9
        Debug.Print thelist(ix)
    Next ix
End Sub
'
' QSORT modified to handle public MyType indexes as elements
'
' The SortList passed in (and recursed) is an index on a vector
' of MyType structures.  The COMPARISON is done on the actual
' internal MyType element which is the sort key (S2 in this case)
' thus when the QSORT is finished, the SortList is a vector of
' indexes which can then be used to get at the MyType vector
' in sorted order.
'
Public Sub QuickSort2(SortList As Variant, ByVal First As Integer, ByVal Last As Integer)
    Dim Low As Integer, High As Integer         'Use Integer for lists up to 32,767 entries.
    Dim temp1 As Variant, TestElement As Variant 'Variant can handle any type of list.
    Low = First
    High = Last
    TestElement = lst(SortList((First + Last) / 2)).S2  'Select an element from the middle.
    Do
        Do While lst(SortList(Low)).S2 < TestElement    'Find lowest element that is >= TestElement.
            Low = Low + 1
        Loop
        Do While lst(SortList(High)).S2 > TestElement   'Find highest element that is <= TestElement.
            High = High - 1
        Loop
        If (Low <= High) Then                   'If not done,
            temp1 = SortList(Low)                ' Swap the elements.
            SortList(Low) = SortList(High)
            SortList(High) = temp1
            Low = Low + 1
            High = High - 1
        End If
    Loop While (Low <= High)
    If (First < High) Then QuickSort2 SortList, First, High
    If (Low < Last) Then QuickSort2 SortList, Low, Last
End Sub
[/vbcode]
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Accepted Solution

by:
Julian_K earned 300 total points
ID: 7075610
Hi.
You mean creating a binary tree, ect?
0
 
LVL 5

Expert Comment

by:Julian_K
ID: 7075653
Hi again.
By the way, If you need just a FAST algorythm, and NOT ONLY a binary tree, I have one method handy.

It makes approximately:
Log(2)X comparitions
to find the place of an element in an array of X elements.
Say It's an array of 1024 elements, it will do 10 comparitions; or 20 comparitions for an array of 2^20 elements.

So, to sort ALL elements in an array, it takes:
Sum(1 to x) of Log2(I) comparitions
to sort X elements (where I is from 1 to X)

0
 
LVL 5

Expert Comment

by:Julian_K
ID: 7211081
Hello again. I had forgoten this question, sorry...
Here is the method I often use to sort arrays.
I have moved the comparition to a procedure, so you could change it according to your needs - you may be comparing more than just one value.
"Command1_Click" - just to test it.
I'm sure this method could be improved, but I don't have time to think of it now.
I hope this will help You.

Private Sub Command1_Click()
    SortArray (Array(1, 9, 17, 5, 2, 7, 4, 3, 6, 8, 10, 15, 16, 14, 12, 13, 18, 11))
End Sub

Private Sub SortArray(ByRef vntArr As Variant)
       
        Dim lngLLimit As Long     'lower array limit
        Dim lngTotalItems As Long 'upper array limit
       
        Dim lngULimit As Long   'upper limit of sorted items
       
        Dim lngCurrent As Long  'current item being sorted
        Dim lngChecked As Long  'item being checked with the current
       
        Dim lngLSeekLimit As Long   'last smaller detected item
        Dim lngUSeekLimit As Long   'last bigger detected item
       
        Dim I As Long, vntItem As Variant   'used to move items
       
        Dim lngCounter As Long  'just to count comparition operations, you can remove it.
       
   
    lngTotalItems = UBound(vntArr)  'get upper bound of the array
    lngLLimit = LBound(vntArr)      'get lower bound of the array
    lngULimit = lngLLimit + 1       'sorted items in the array (=2)
   
    'sort first and second items in the array
    If IsBigger(vntArr(lngLLimit), vntArr(lngULimit)) Then
        vntItem = vntArr(lngLLimit)
        vntArr(lngLLimit) = vntArr(lngULimit)
        vntArr(lngULimit) = vntItem
    End If
   
   
    'repeat until the last item is sorted
    Do Until lngULimit = lngTotalItems
   
        lngCurrent = lngULimit + 1  'this item will be sorted
        lngLSeekLimit = lngLLimit   'this is the smaller known item
        lngUSeekLimit = lngULimit   'this is the biggest known item
       
        lngCounter = 0  'reset comparitions counter
       
        'find place where then new item will be moved to
        Do
            'get middle item of the sorted items list
            lngChecked = (lngUSeekLimit - lngLSeekLimit) \ 2 + lngLSeekLimit
           
            lngCounter = lngCounter + 1
           
            'if the new item is bigger than the middle element, change boundaries
            If IsBigger(vntArr(lngCurrent), vntArr(lngChecked)) Then
                lngLSeekLimit = lngChecked + 1
            Else
                lngUSeekLimit = lngChecked - 1
            End If
        Loop Until lngLSeekLimit > lngUSeekLimit
       
        'move the new item to its proper place
        vntItem = vntArr(lngCurrent)
        For I = lngULimit To lngLSeekLimit Step -1
            vntArr(I + 1) = vntArr(I)
        Next I
        vntArr(lngLSeekLimit) = vntItem
       
        'display operation on the screen. This code is for demo only - remove it.
        For I = lngLLimit To lngTotalItems
            Debug.Print vntArr(I) & "  ";
            If I = lngCurrent Then Debug.Print "/ ";
        Next I
        Debug.Print " -> [" & lngCounter & " comparitions]"
       
        'increase the sorted items counter
        lngULimit = lngULimit + 1
    Loop
   
End Sub

Private Function IsBigger(ByVal vntFirst As Variant, ByVal vntSecond As Variant) As Boolean
    IsBigger = (vntFirst > vntSecond)
End Function
0
 
LVL 5

Expert Comment

by:Julian_K
ID: 7211097
You can speed it up, if you use second array to store results into - this way it won't be neccesary to move the elements to allocate space for each new sorted item.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

801 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