[prev] 23 [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
|  while left<right do
|  |  left  = find index of leftmost element > pivot_item    // left=high if none
|  |  right = find index of rightmost element <= pivot_item
|  |  if left<right then
|  |     swap array[left] with array[right]
|  |  end if
|  end while
|  array[low]=array[right] // right is final position for pivot
|  array[right]=pivot_item
|  return right