Results 1 to 7 of 7
  1. #1
    Star Lounger
    Join Date
    Apr 2004
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Quick Sort (VB6)

    (Edited by HansV to provide links to posts - see <!help=19>Help 19<!/help>)

    Hi

    This leads on from <post#=461805>post 461805</post#> re populating an array from a text file. Well I have investigated all sorts of Sorts and given the size of my Array have decided that the Quick Sort would be the fastest/most efficient. Following Hans V's info I looked at <post#=134856>post 134856</post#> and have added this to my project. The only two things I have changed is that in the source code :

    Sub QuickSort(a() As Integer I have put
    Sub QuickSort(Dictionary()as String - Dictionary being the name of my String Array

    Now for the usual embarrassing bit. It might work but I can't seem to Call it. Whatever I seem to do either it (a) compiles but nothing happens: in that my test to check what is in the array doesn't show anything or I get a compile error: Argument not optional

    Appreciate I am missing the b.obvious but at present can't see that this is.

    Please (yet again) sort me out.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Quick Sort (VB6)

    I'm not clear on how a quicksort works: does the procedure need to call itself over and over to iterate through the array, or does it only get called once? Let's assume the simple case:

    Sub Test()
    QuickSort myArray()
    End Sub

    Of course, if you plan to do something with the array after it is sorted, that would go before the End Sub

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Quick Sort (VB6)

    You must also modify bits of the code in QuickSort:

    Sub QuickSort(a() As String, ByVal Low As Long, ByVal Hi As Long)
    ' Very fast sort: n Log n comparisons
    ' Calling convention:
    ' Redim a(1 To 20) as String
    ' QuickSort a(), 1, 20
    Dim MidValue As String, i As Long, j As Long, Temp As String
    If Hi <= Low Then Exit Sub
    MidValue = a((Low + Hi) 2)
    i = Low
    j = Hi
    Do While i <= j
    If a(i) >= MidValue And a(j) <= MidValue Then
    Temp = a(i)
    a(i) = a(j)
    a(j) = Temp
    i = i + 1
    j = j - 1
    Else
    If a(i) < MidValue Then i = i + 1
    If a(j) > MidValue Then j = j - 1
    End If
    Loop
    QuickSort a(), Low, j
    QuickSort a(), i, Hi
    End Sub

    Use it like this:

    Dim Dictionary(cMin To cMax) As String
    ...
    QuickSort Dictionary(), cMin, cMax
    ...

    After the line with QuickSort, the Dictionary array will be sorted. As noted in <post#=134856>post 134856</post#>, applying QuickSort directly to an array of strings is not optimal, but see if it works for you.

  4. #4
    Star Lounger
    Join Date
    Apr 2004
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Quick Sort (VB6)

    Sorry Hans

    Another couple of hours of messing about and I still can't seem to get this to work...

    I think I understand the amendments to the code okay, but I am obviously misinterpreting your 'Use it like this' instructions...

    I have
    Dim Dictionary (cMin To cMax) As String
    in general declarations

    The code is now as per your latest post except every incidence of a is now Dictionary

    There is a clickevent cmdSort
    QuickSort Dictionary(), cMin, cMax
    lblResult.Caption = Dictionary(10) 'Used to check that sort has actioned.

    Everything compiles okay. but I don't get any results returned to check.

    Thanks very much for all your help with this. I did note the comment on post 134856 re sorting on index numbers rather than the strings themselves. My plan was to get this bit working before branching out into something more complicated.

    p.s. Another bit of info I couldn't seem to find anywhere. When you load an Array does it stay loaded when you exit the program?

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Quick Sort (VB6)

    Are you sure that you have populated the Dictionary array before you sort it? If you haven't, all elements will be empty strings.

    The "lifetime" of a variable depends on where and how it is defined.
    - A variable defined within a procedure (sub) or function only exists when the procedure/function is being executed. It vanishes as soon as the procedure/function finishes.
    - An exception is a variable defined within a procedure or function with the keyword Static. Such a variable retains its value between runs of the procedure/function, but it is not available outside the procedure/function. It vanishes when the application quits.
    - A variable defined at the top of a module exists while the application runs. It vanishes when the application quits.
    - All variables are reset to their default value (0 for numbers, "" for strings, False for booleans, Nothing for objects) if an unhandled error occurs.

  6. #6
    Star Lounger
    Join Date
    Apr 2004
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Quick Sort (VB6)

    Hi

    A ton of research and I've finally got to the bottom of this.

    The ShellSort program and the number of elements in the Array MUST match if the thing is to work properly. This is the reason ShellSort programs are usually used with dynamic arrays.

    If you declare a static array and have extra 'headroom' then the empty elements will get sorted to the top (or bottom if you're doing a reverse sort). This was why when I was checking for the entry in element 10 it was blank. Once I had set cMin and cMax to the exact number of elements in the Array - BINGO!

    One last point to mention with this. ShellSort uses different criteria to MSExcel to sort A-Z when 'odd' characters are involved. Without further investigation I am guessing that this is something to do with ANSI/ASCII codes. Not sure this matters in the great scheme of things but it does throw things out when you're testing your results.

    Am treating myself to a smily, because I actually think I might have finally contributed something <img src=/S/bow.gif border=0 alt=bow width=15 height=15>

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Quick Sort (VB6)

    The code I posted uses a simple < to compare elements. You can control how it operates by placing an Option Compare line at the top of the module (Option Compare Text or Option Compare Binary - see Option Compare Statement), or by using StrComp, this function has a compare argument - see StrComp Function.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •