Results 1 to 3 of 3

Thread: Array sorting for user-defined data types

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Albany, NY
    Posts
    489

    QuickSort

    I got the QuickSort code from somewhere??
    here is a quick example.

    VB Code:
    1. Private Type Descriptions
    2.     strDesc As String
    3.     strCode As String
    4. End Type
    5.  
    6. Private Sub Quicksort(ByRef p_Array() As Descriptions, ByVal inLow As Long, ByVal inHi As Long)
    7. 'A version of the QuickSort Algorithm applied to the User Defined Data Type "Descriptions"
    8.    Dim pivot   As Descriptions
    9.    Dim tmpSwap As Descriptions
    10.    Dim tmpLow  As Long
    11.    Dim tmpHi   As Long
    12.    
    13.    tmpLow = inLow
    14.    tmpHi = inHi
    15.    
    16.    pivot = p_Array((inLow + inHi) \ 2)
    17.  
    18.    While (tmpLow <= tmpHi)
    19.  
    20.       While (p_Array(tmpLow).strDesc < pivot.strDesc And tmpLow < inHi)
    21.          tmpLow = tmpLow + 1
    22.       Wend
    23.      
    24.       While (pivot.strDesc < p_Array(tmpHi).strDesc And tmpHi > inLow)
    25.          tmpHi = tmpHi - 1
    26.       Wend
    27.  
    28.       If (tmpLow <= tmpHi) Then
    29.          tmpSwap = p_Array(tmpLow)
    30.          p_Array(tmpLow) = p_Array(tmpHi)
    31.          p_Array(tmpHi) = tmpSwap
    32.          tmpLow = tmpLow + 1
    33.          tmpHi = tmpHi - 1
    34.       End If
    35.    
    36.    Wend
    37.  
    38.    If (inLow < tmpHi) Then Quicksort p_Array, inLow, tmpHi
    39.    If (tmpLow < inHi) Then Quicksort p_Array, tmpLow, inHi
    40.  
    41. End Sub

  2. #2
    New Member
    Join Date
    Jun 2009
    Posts
    1

    Re: Array sorting for user-defined data types

    An interesting piece of code...
    The last 2 lines puzzle me, since they it seems like a circular reference
    to sub Quicksort, namely
    If (inLow < tmpHi) Then Quicksort p_Array, inLow, tmpHi
    If (tmpLow < inHi) Then Quicksort p_Array, tmpLow, inHi

    Also, can you provide a fuller sample code with the end result being a sorted
    array Descriptions().

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Array sorting for user-defined data types

    The last 2 lines puzzle me, since they it seems like a circular reference
    that is correct it is a recursive procedure the array is not sorted in one pass so the sub is continuously called until fully sorted
    there is several examples in code bank that may contain more information

    @OP is there a question, or is it all working correctly?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width