Quicksort(A, low, high) // low is the index of the first item to be sorted. // high is the index of the last item to be sorted.
- pivot = Partition(A, low, high)
- Quicksort(A, low, partition-1)
- Quicksort(A, partition, high)
int Partition(A, low, high)
- pivot = A[(low+high/2)]
- lowpos = low-1
- highpos = high+1
- While lowPos < highPos
- Repeat
- lowPos++
- Until A[lowPos] >= pivot
- Repeat
- highPos--
- Until A[highPos] <= pivot
- If LowPos < HighPos
- swap(A[lowPos], A[highPos])
- Endif
- EndWhile
- return highPos
Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
---|---|---|---|---|---|---|---|---|---|---|---|
PrePart | 4 | 12 | 6 | 15 | 4 | 9 | 6 | 27 | 8 | 24 | 5 |
PostPart |
Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
---|---|---|---|---|---|---|---|---|---|---|---|
PrePart | 1 | 4 | 5 | 8 | 10 | 11 | 3 | 7 | 2 | 9 | 6 |
PostPart |