1 / 7

QuickSort Example

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.

ermin
Download Presentation

QuickSort Example

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  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 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.

  3. 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.

  4. 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.

  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 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.

  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, 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.

  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 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.

More Related