[prev] 26 [next]

Non-randomised Quicksort

Divide ...

partition(array,low,high):
|  Input  array, index range low..high
|  Output selects array[low] as pivot element
|         moves all smaller elements between low+1..high to its left
|         moves all larger elements between low+1..high to its right
|         returns new position of pivot element
|
|  pivot_item=array[low], left=low+1, right=high
|  repeat
|  |  right = find index of rightmost element <= pivot_item
|  |  left  = find index of leftmost element > pivot_item    // left=right if none
|  |  if left<right then
|  |     swap array[left] with array[right]
|  |  end if
|  until left≥right
|  if low<right then
|     swap array[low] with array[right]       // right is final position for pivot
|  end if
|  return right