130 likes | 337 Views
QuickSort Example. 13, 21, 15, 3, 12, 9, 14, 7, 6. 3,. 3, 9,. 3, 9, 7,. 3, 9, 7, 6,. 21 > 12 so we don’t write it down. 3 < 12 so we do write it down. We ignore the pivot for the moment and move on. 9 < 12 so we do write it down.
E N D
QuickSort Example 13, 21, 15, 3, 12, 9, 14, 7, 6 3, 3, 9, 3, 9, 7, 3, 9, 7, 6, 21 > 12 so we don’t write it down 3 < 12 so we do write it down We ignore the pivot for the moment and move on. 9 < 12 so we do write it down. 13 > 12 so we don’t write it down 15 > 12 so we don’t write it down 14 > 12 so we don’t write it down 7 < 12 so we do write it down 6 < 12 so we do write it down First we use the number in the centre of the list as a ‘pivot’. We then divide the list into those that are smaller than the pivot and those that are larger. First we write a list of the numbers smaller than the pivot.
QuickSort Example 13, 21, 15, 3, 12, 9, 14, 7, 6 3, 9, 7, 6, 12, 3, 9, 7, 6, 12, 13, 3, 9, 7, 6, 12, 13, 21, 3, 9, 7, 6, 12, 13, 21, 15 3, 9, 7, 6, 12, 13, 21, 15, 14 3, 9, 7, 6, We have written down all the numbers less than the pivot. Now we can write down the pivot in its correct position. We now go through the list again and write down all the numbers greater than the pivot.
QuickSort Example 13, 21, 15, 3, 12, 9, 14, 7, 6 3, 9, 7, 6, 12, 13, 21, 15, 14 3, 6, 3, 6, 7, 9, 3, 6, 7, We now repeat the procedure on the first half of the list. First we select a pivot. We take the number 7 to the right of centre. We write down the numbers smaller than 7 Now we write down the pivot. Now the numbers greater than 7.
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, 13, 14, 15, 21 13, 14, 15, Moving on to the second half. We take the number 15 to the right of centre as the pivot. We write down the numbers smaller than 15 Now we write down the pivot. Now the numbers greater than 15.
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 3, 3, 6, 7, 9 3, 6, The list is now sorted but the algorithm keeps going until lists of length 1 are reached. First list has pivot 6. Less than pivot. Moving on 9 is a list on its own. Now the pivot.
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 3, 6, 7, 9, 12, 3, 6, 7, 9, 12, 13, 14, 15, 3, 6, 7, 9, 12, 13, 14, 15, 21 The next list is easily dealt with. Finally the last list.
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 3, 6, 7, 9, 12, 13, 14, 15, 21 3, 6, 7, 9, 12, 13, 14, 15, 21 Only two lists of length one remain to be dealt with. The algorithm finishes when all the numbers have been considered as pivots.