Download presentation
Presentation is loading. Please wait.
Published byMontana Deakin Modified over 10 years ago
1
QuickSort Example 13, 21, 15, 3, 12, 9, 14, 7, 6 3, 3, 9, 3, 9, 7, 3, 9, 7, 6, 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. 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. 13 > 12 so we don’t write it down 21 > 12 so we don’t write it down 15 > 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. We ignore the pivot for the moment and move on. 9 < 12 so we do 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
2
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 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. 3, 9, 7, 6,
3
QuickSort Example 13, 21, 15, 3, 12, 9, 14, 7, 6 3, 9, 7, 6, 12, 13, 21, 15, 14 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 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 3, 6, Now we write down the pivot. 3, 6, 7, 9, 3, 6, 7, Now the numbers greater than 7.
4
QuickSort Example 13, 21, 15, 3, 12, 9, 14, 7, 6 3, 9, 7, 6, 12, 13, 21, 15, 14 Moving on to the second half. We take the number 15 to the right of centre as the pivot. 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 13, 14, Now we write down the pivot. 13, 14, 15, 21 13, 14, 15, Now the numbers greater than 15. 3, 6, 7, 9, 12
5
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 The list is now sorted but the algorithm keeps going until lists of length 1 are reached. First list has pivot 6. The list is now sorted but the algorithm keeps going until lists of length 1 are reached. First list has pivot 6. 3, Less than pivot. 3, 6, 7, 9 Moving on 9 is a list on its own. 3, 6, Now the pivot.
6
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, The next list is easily dealt with. 3, 6, 7, 9, 12, 13, 14, 15, Finally the last list. 3, 6, 7, 9, 12, 13, 14, 15, 21
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, 15, 21 Only two lists of length one remain to be dealt with. 3, 6, 7, 9, 12, 13, 14, 15, 21 The algorithm finishes when all the numbers have been considered as pivots.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.