• Quick Sort (VB6)

    Author
    Topic
    #417234

    (Edited by HansV to provide links to posts – see Help 19)

    Hi

    This leads on from post 461805 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 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.

    Viewing 1 reply thread
    Author
    Replies
    • #935531

      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

    • #935538

      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 = 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 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, applying QuickSort directly to an array of strings is not optimal, but see if it works for you.

      • #935570

        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?

        • #935573

          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.

          • #935802

            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 bow

            • #935835

              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.

    Viewing 1 reply thread
    Reply To: Quick Sort (VB6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: