Quick Sort
Quick Sort
QuickSort Example
13, 21, 15, 3, 12, 9, 14, 7, 6 15 14 3, 9, 7, 6, 12, 13, 21, 15,
We have down all again the numbers less than the pivot. Nowgreater we canthan write We now go written through the list and write down all the numbers the pivot. down the pivot in its correct position.
QuickSort Example
13, 21, 15, 3, 12, 9, 14, 7, 6 3, 9, 7, 6, 12, 13, 21, 15, 14 3, 6, 7, 9,
We now repeat the procedure on the first half of the list. First we write select a pivot. take We write Now we down down the numbers the We pivot. Now thethan numbers greater than 7. the number 77to the right of centre. smaller
QuickSort Example
13, 21, 15, 3, 12, 9, 14, 7, 6 3, 9, 7, 6, 12, 13, 21, 15, 14 3, 6, 7, 9, 12 13, 14, 15, 21
Moving on to the second half.
We take the number 15 to the right of centre as the pivot. We Now write we down down thegreater numbers the pivot. Now the write numbers than 15. smaller than 15
QuickSort Example
13, 3, 3, 3, 21, 9, 6, 6, 15, 7, 7, 7, 3, 12, 9, 14, 7, 6 6, 12, 13, 21, 15, 14 9, 12, 13, 14, 15, 21 9
The list is now sorted but the algorithm keeps going until lists of length 1 are reached. Less Moving Nowlist than the on pivot. pivot. 9 is a list First has pivot 6. on its own.
QuickSort Example
13, 3, 3, 3, 21, 9, 6, 6, 15, 7, 7, 7, 3, 6, 9, 9, 12, 12, 12, 12, 9, 14, 13, 21, 13, 14, 13, 14, 7, 6 15, 14 15, 21 15, 21
QuickSort Example
13, 3, 3, 3, 3, 21, 9, 6, 6, 6, 15, 7, 7, 7, 7, 3, 6, 9, 9, 9, 12, 12, 12, 12, 12, 9, 14, 13, 21, 13, 14, 13, 14, 13, 14, 7, 6 15, 14 15, 21 15, 21 15, 21
The algorithm finishes when all the numbers have been considered as pivots.