You are on page 1of 8

# Quick sort explained

Quick Sort
Quick sort was discovered by Tony Hoare in 1960. In general it has much better performance than brute force
sorting methods like bubble sort or selection sort - O(n log2 n) versus O(n2) . It works by first of all by
partitioning the array around a pivot value and then dealing with the 2 smaller partitions separately.
Partitioning is the most complex part of quick sort. The simplest thing is to use the first value in the array, a[l]
(or a as l = 0 to begin with) as the pivot. After the partitioning, all values to the left of the pivot are <= pivot
and all values to the right are > pivot.
For example, consider

k
a[k]

l
0
8

1
2

2
5

3
13

4
4

5 6
19 12

7
6

8
3

r
9 10 11 12
11 10 7 9

where the value of a[l], namely 8, is chosen as pivot. Then the partition function moves along the array from
the lhs until it finds a value > pivot. Next it moves from the rhs, passing values > pivot and stops when it finds
a value <= pivot. This is done by the following piece of code:
i = l; j = r+1;
do ++i;
while( a[i] <= pivot && i <= r );
do --j;
while( a[j] > pivot );
Then if the lhs value is to the left of the rhs value, they are swapped. Variable i keeps track of the current
position on moving from left to right and j does the same for moving from right to left. You then get
a[k]

l
8

2

5

i
13

5

i
7

4

19 12

3

11 10

j
7

r
9

6

3

j
11 10 13

r
9

6

j
3

11 10 13

r
9

6

a[i] and a[j] are swapped to give
a[k]

l
8

2

4

19 12

4

i
19 12

4

i
3

This process is repeated with i and j to get
a[k]

l
8

2

5

7

and do a swap to get
l
a[k] 8

2

5

7

12 6

1

j
r
19 11 10 13 9

0. The pivot is left alone as it is in the correct position. 7. At this stage the partition code breaks out of the main while loop. The lhs array is similarly partitioned from l 6 r 2 5 7 i 7 i 3 l 6 2 5 l 6 2 5 l 6 2 5 3 4 5 3 3 rj 4 4 j 2 3 rj j l 4 4 6 7 i r 7 i r 7 Next do quickSort( a. 5) – no more partition in rhs subarray. It is a waste of computation power to partition an array of size 2 and call quickSort twice more. 2 . All a[k] <= pivot where k <= j. 0. l. 3) quickSort( a. 5) quickSort( a. 5. j-1) or quickSort( a.Quick sort explained Move i and j again to get a[k] l 8 a[k] l 8 a[k] l 8 2 5 7 4 3 i 12 j 6 19 11 10 13 r 9 3 i 6 j 12 19 11 10 13 r 9 3 j 6 i 12 19 11 10 13 r 9 and swap again 2 5 7 4 Move i and j again. So quickSort divides the problem into two smaller ones and recursively calls itself on each of then as in: quickSort( a. 2 5 7 4 When j passes i. 12) The smaller arrays are again partitioned in the same way as described above and so on. j+1. All that we do next is swap the pivot with a[j] to get j 8 l a[k] 6 2 5 7 4 3 i 12 19 11 10 13 r 9 Then quickSort is called again separately on the two smaller arrays (here a to a and a to a). Eventually quickSort will arrive at arrays of size 1 or 2 or 0. r) or quickSort( a. Arrays of size 0 or 1 are already so to say sorted. while an array of size 2 can be sorted with an if statement. the partitioning is finished.

0. 0) – no more partition in lhs subarray. 12)is tackled which partitions subarray as follows: 13 19 13 19 All the partitioning in now over with the result that in the meantime the array has been sorted. The lhs subarray is again partitioned and a swap done 3 2 2 3 Next do quickSort( a. 7. 3) is tackled which partitions subarray as follows: 4 4 4 3 2 2 2 2 5 5 3 4 3 3 5 5 Next do quickSort( a. 0. 12). 0. Number of computations: I counted: comparisons = 40 swaps = 13 3 . 9)is tackled which partitions subarray as follows: 10 9 11 9 10 11 No further partitioning occurs on these two subarrays. 11. 1) quickSort( a. 7. Next quickSort( a.Quick sort explained Next quickSort( a. 11. 3. 3) – no more partition in rhs subarray. 7. Next quickSort( a. 12) which partitions subarray as follows: 12 19 11 10 13 9 12 19 11 10 13 9 12 9 11 10 13 19 10 9 11 12 13 19 and calls quickSort( a. 9) and quickSort( a. Next we return to the original lhs subarray from the first partition quickSort( a.

4 .Quick sort explained Overview 2 8 2 5 4 3 6 2 5 7 4 2 5 3 3 2 13 19 12 6 3 8 6 4 4 7 10 5 11 12 19 9 11 9 10 10 11 7 9 10 13 12 13 11 13 9 19 19 3 2 3 4 5 6 7 8 9 10 11 12 13 19 Note: Quick sort works its way down through partitions. creating smaller ones all the time before it deals with all partitions on a given level. This is referred to as a depth-first approach.

int. 9}. printf("\n\nUnsorted array is: for(i = 0. j+1. 0. "). i < 9. 0. 1. } void quickSort( int a[].h> void quickSort( int[]. 12. 8).c // quickSort. int). ++i) printf(" %d ". int). if( l < r ) { // divide and conquer j = partition( a. a[i]).Quick sort explained quickSort. "). printf("\n\nSorted array is: for(i = 0. 15. a[i]). int. } } 5 . int r) { int j. ++i) printf(" %d ". int i. void main() { int a[] = { 7. quickSort( a. -2. 4. int partition( int[].cpp #include <stdio. r). l. quickSort( a. i < 9. r). quickSort( a. 11. int l. j-1). l.

return j. a[j] = temp. pivot = a[l]. do --j. } temp = a[l]. a[l] = a[j]). temp = a[i]. i = l. } 6 . int l. i. while( a[j] > pivot ). if( i >= j ) break.Quick sort explained int partition( int a[]. a[j] = temp. while( 1) { do ++i. a[i] = a[j]). temp. int r) { int pivot. j. j = r+1. while( a[i] <= pivot && i <= r ).

9}. 0. } t = a[l]. 15. j = r+1. i < 9. i = l. quickSort( a. t. 12. 11. r). quickSort( a. a[i]). do --j. int i. t = a[i]. } } 7 .h> void quickSort( int[]. "). j-1). void main() { int a[] = { 7. "). 4. printf("\n\nSorted array is: for(i = 0. i < 9. a[l] = a[j]. quickSort( a. int. int r) { int pivot. j+1. ++i) printf(" %d ". while( 1) { do ++i. } void quickSort( int a[]. while( a[i] <= pivot && i <= r ). a[j] = t. int l. a[i]). if( i >= j ) break.Quick sort explained Version from Sedgewick // quickSort_Sedgew. j. a[i] = a[j]. 0. 1. l. while( a[j] > pivot ). -2. if( l < r ) { pivot = a[l]. printf("\n\nUnsorted array is: for(i = 0. int). i. 8).cpp #include <stdio. a[j] = t. ++i) printf(" %d ".

quickSort( a. r). a[j] = t. } return. j+1.Quick sort explained quickSort_Ver2. a[l] = a[j]. // array of size 2. // sub array of size 1 or 0 . l. a[l] = a[r]. while( a[i] <= pivot && i <= r ). } pivot = a[l]. int l. i. int r) { int pivot.cpp This version is more efficient when dealing with small subarrays. sort directly with if if( l+1 == r) { if(a[l] > a[r]) { t = a[l]. // quickSort_Ver2. t. quickSort( a.cpp // more efficient near end of sorting // main() etc left out void quickSort( int a[]. t = a[i]. j = r+1. if( i >= j ) break. j-1). while( a[j] > pivot ). } 8 . do --j.already sorted if(l >= r ) return. i = l. a[j] = t. while( 1) { do ++i. j. a[i] = a[j]. a[r] = t. } t = a[l].