Abundant Technologies - IT Consulting Experts

 Source Code Library

 Code Details

' ============================ QuickSort ============================       ' QuickSort works by picking a random "pivot" element in SortArray,       ' then moving every element that is bigger to one side of the pivot,       ' and every element that is smaller to the other side. QuickSort is       ' then called recursively with the two subdivisions created by the       ' pivot. Once the number of elements in a subdivision reaches two,       ' the recursive calls end and the array is sorted.       ' ===================================================================       '       Private Sub QuickSort(SortArray() As String, ByVal Low As Long, _                          ByVal High As Long)          Dim I As Long, J As Long, RandIndex As Long, Partition As String          If Low < High Then             ' Only two elements in this subdivision; swap them if they are             ' out of order, then end recursive calls:             If High - Low = 1 Then                If UCase(SortArray(Low)) > UCase(SortArray(High)) Then                   SWAP SortArray(Low), SortArray(High)                End If             Else                ' Pick a pivot element at random, then move it to the end:                RandIndex = Rnd() * (High - Low) + Low ' RandInt%(Low, High)                SWAP SortArray(High), SortArray(RandIndex)                Partition = UCase(SortArray(High))                Do                   ' Move in from both sides towards the pivot element:                   I = Low: J = High                   Do While (I < J) And (UCase(SortArray(I)) <= Partition)                      I = I + 1                   Loop                   Do While (J > I) And (UCase(SortArray(J)) >= Partition)                      J = J - 1                   Loop                   ' If we haven't reached the pivot element it means that 2                   ' elements on either side are out of order, so swap them:                   If I < J Then                      SWAP SortArray(I), SortArray(J)                   End If                Loop While I < J                ' Move the pivot element to its proper place in the array:                SWAP SortArray(I), SortArray(High)                ' Recursively call the QuickSort procedure (pass the                ' smaller subdivision first to use less stack space):                If (I - Low) < (High - I) Then                   QuickSort SortArray, Low, I - 1                   QuickSort SortArray, I + 1, High                Else                   QuickSort SortArray, I + 1, High                   QuickSort SortArray, Low, I - 1                End If             End If          End If       End Sub         Private Sub SWAP(first As String, second As String)          Dim temp As String          temp = first          first = second          second = temp       End Sub

Mail To: info@3pc.com