Hi Again,

    This is the quick sort routine for a 2 dimensional array. It also has a 
one dimensional array commented out; note: dividing by half does not work, a 
random number does.

    So, here you have the Quick Sort routine that can be changed in the 
calling list at the name to a one dimensional array and what you need along 
with the call to quicksort at the end which has to match the prototype at 
the top.

    So comment out the 2 dimensional stuff and uncomment the original one 
dimensional and you have the quick sort both ways.

        Sincerely
        Bruce

' *******************************************************
' Recursively perform the actual quicksort algorithm.
' Do a 2 dimensional column sort where first column is row value and second 
element is column element value.
' The column dimension allows many values in list, such as name, date, 
version, size, and and thing, like the UnInstall program.
' *******************************************************
Sub Quicksort (List, min, max, sortCol, maxCols)
 Dim midRow(), midValue, hi, lo, i
 ReDim midRow(1, maxCols)
 ' If the list has no more than 1 element, it's sorted.
 If min >= max Then Exit Sub
 ' Pick a dividing item.
' i = Int( (max - min) /2)
' i = Int( (max - min + 1) * Rnd + min)
 i = Int( (max - min) * Rnd + min)
 midValue = List( i, sortCol)
 assignRow midRow, 0, List, i, maxCols
 ' Swap it to the front so we can find it easily.
' List( i) = List( min)
 assignRow List, i, List, min, maxCols
 ' Move the items smaller than this into the left half of the list. Move the 
others into the right.
 lo = min
 hi = max
 Do
  ' Look down from hi for a value < midValue.
  Do While List( hi, sortCol) >= midValue
   hi = hi - 1
   If hi <= lo Then Exit Do
  Loop
  If hi <= lo Then
'   List( lo) = midValue
   assignRow List, lo, midRow, 0, maxCols
   Exit Do
  End If
  ' Swap the lo and hi values.
  'List( lo) = List( hi)
  assignRow List, lo, List, hi, maxCols
          ' Look up from lo for a value >= midValue.
  lo = lo + 1
  Do While List( lo, sortCol) < midValue
   lo = lo + 1
   If lo >= hi Then Exit Do
  Loop
  If lo >= hi Then
   lo = hi
   'List( hi) = midValue
   assignRow List, hi, midRow, 0, maxCols
   Exit Do
  End If
  ' Swap the lo and hi values.
  'List(hi) = List(lo)
  assignRow List, hi, List, lo, maxCols
 Loop
         ' Sort the two sublists
 Quicksort List, min, lo - 1, sortCol, maxCols
 Quicksort List, lo + 1, max, sortCol, maxCols
End Sub

Sub assignRow( destinationList, destinationRow, sourceList, sourceRow, 
maxCol)
 ' Assign source list row to destination list row.
 Dim c
 For c = 0 to maxCol
  destinationList( destinationRow, c) = sourceList( sourceRow, c)
 Next
End Sub

Sub swapListRows( row1, row2, maxCol)
 ' Swap the 2 list rows.
 Dim c, temp
 For c = 0 to maxCol
  temp = list( row1, c)
  list( row1, c) = list( row2, c)
  list( row2, c) = temp
 Next
End Sub


Reply via email to