WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Sort

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

QuickSort

 Keywords: Array Sort Two Dimensional QuickSort Algorithm 

;*************************************************************************
;
;   Array sort for two-dimensional arrays. Uses the "QuickSort" algorithm.
;
;   Array ....... The array to be sorted
;   Key1, Key2, Key3 ... The columns to be used as the sort keys
;
;   On return, the original array has been sorted.
;   (The actual return value is always zero, and may be ignored.)
;
;   NOTE that this sort method will not preserve the original
;   order of items with equal keys.
;
;   Developer: Alan  Kreutzer
;
;**************************************************************************
#DefineFunction ArraySort(Array, StartRow, Key1, Key2, Key3)
  TopRow = ArrInfo(Array,1)-1
  QUICKSORT(Array,StartRow,TopRow, Key1, Key2, Key3)
  Return
#EndFunction

#DefineFunction QUICKSORT(Array, LowBound, HighBound, Key1, Key2, Key3)
  If HighBound <= LowBound Then Return
  MaxColumn = ArrInfo(Array,2) -1
  xLow = LowBound
  xHigh = HighBound
  xPivot = Int((LowBound + HighBound) / 2)
  Pivot1 = Array[xPivot,Key1]
  Pivot2 = Array[xPivot,Key2]
  Pivot3 = Array[xPivot,Key3]
  While (xLow <= xHigh)
     While QuickSortIsLower(Array[xLow,Key1],Array[xLow,Key2],Array[xLow,Key3],Pivot1,Pivot2,Pivot3)
          xLow = xLow + 1
          If xLow == HighBound Then Break
     EndWhile
     While QuickSortIsLower(Pivot1,Pivot2,Pivot3,Array[xHigh,Key1],Array[xHigh,Key2],Array[xHigh,Key3])
          xHigh = xHigh - 1
          If xHigh == LowBound Then Break
     EndWhile
     If  xLow < xHigh
        For xCol = 0 To MaxColumn
           Temp = Array[xLow,xCol]
           Array[xLow,xCol] = Array[xHigh,xCol]
           Array[xHigh,xCol] = Temp
        Next
     EndIf
     If xLow <= xHigh
        xLow = xLow + 1
        xHigh = xHigh - 1
     End If
  EndWhile
  QUICKSORT(Array,LowBound, xHigh, Key1, Key2, Key3)
  QUICKSORT(Array,xLow, HighBound, Key1, Key2, Key3)
#EndFunction

#DefineFunction QuickSortIsLower(Arr1,Arr2,Arr3,Pivot1,Pivot2,Pivot3)
  If Arr1 < Pivot1 Then Return @TRUE
  If Arr1 > Pivot1 Then Return @FALSE
  If Arr2 < Pivot2 Then Return @TRUE
  If Arr2 > Pivot2 Then Return @FALSE
  If Arr3 < Pivot3 Then Return @TRUE
  Return @FALSE
#EndFunction


Article ID:   W17678
Filename:   QuickSort.txt
File Created: 2009:07:13:09:33:00
Last Updated: 2009:07:13:09:33:00