QuickSort Example Use the first number in the list as a ‘pivot’. 13211591231476 First write a...
-
Upload
barbra-wiggins -
Category
Documents
-
view
223 -
download
2
Transcript of QuickSort Example Use the first number in the list as a ‘pivot’. 13211591231476 First write a...
QuickSort ExampleUse the first number in the list as a ‘pivot’.
13 21 15 9 12 3 14 7 6
First write a list of the numbers smaller than the pivot, in the order in which they appear in the original list.
Divide the list into two sub-lists: those that are smaller than the pivot and those that are larger, as follows:
9 12 3 7 6
9 12 3 7 6
QuickSort ExampleUse the first number in the list as a ‘pivot’.
13 21 15 9 12 3 14 7 6
Next, write down the pivot in its correct position.
Divide the list into two sub-lists: those that are smaller than the pivot and those that are larger, as follows:
9 12 3 7 6
9 12 3 7 6 13
21
QuickSort ExampleUse the first number in the list as a ‘pivot’.
13 21 15 9 12 3 14 7 6
Next, write down the numbers larger than the pivot, in the order in which they appear in the original list.
Divide the list into two sub-lists: those that are smaller than the pivot and those that are larger, as follows:
9 12 3 7 6
9 12 3 7 6 13
21 15
15
14
14
21
QuickSort ExampleThe pivot, 13, is now in its correct position. The next step is to apply the same procedure as before to each of the sub-lists.
13 21 15 9 12 3 14 7 6
Write down the numbers smaller than the pivot, in the order in which they appear in the original sub-list.
Start with the first sub-list, and use the first number in the sub-list as a pivot.
9 12 3 7 6 13 15 143
3
7
7
6
6 13
21
QuickSort Example
13 21 15 9 12 3 14 7 6
Then write down the pivot.
9 12 3 7 6 13 15 143
3
7
7
6
6 9 13
The pivot, 13, is now in its correct position. The next step is to apply the same procedure as before to each of the sub-lists.
Start with the first sub-list, and use the first number in the sub-list as a pivot.
21
QuickSort Example
13 21 15 9 12 3 14 7 6
Then write down the numbers in the sub-list which are greater than the pivot, in the order in which they appear in the original sub-list.
9 12 3 7 6 13 15 143
3
7
7
6
6 9
12
12 13
The pivot, 13, is now in its correct position. The next step is to apply the same procedure as before to each of the sub-lists.
Start with the first sub-list, and use the first number in the sub-list as a pivot.
21
QuickSort Example
13 21 15 9 12 3 14 7 6
9 12 3 7 6 13 15 14
3 7 6 9 12 13
The pivot, 13, is now in its correct position. The next step is to apply the same procedure as before to each of the sub-lists.
Now look at the second sub-list, and use the first number in the sub-list as a pivot.
Write down the numbers smaller than the pivot, in the order in which they appear in the original sub-list.
15
15
14
14
21
QuickSort Example
13 21 15 9 12 3 14 7 6
9 12 3 7 6 13 15 14
3 7 6 9 12 13
The pivot, 13, is now in its correct position. The next step is to apply the same procedure as before to each of the sub-lists.
Now look at the second sub-list, and use the first number in the sub-list as a pivot.
15
15 14
Then write down the pivot.
14
21
In this case there are no numbers greater than the pivot.
21
QuickSort Example
13 21 15 9 12 3 14 7 6
9 12 3 7 6 13 15 14
3 7 6 9 12 13
The numbers 9, 12, 13 and 21 are now in the correct positions. The next step is to apply the same procedure to any sub-lists containing more than one number.
Look at the first sub-list, and use the first number in the sub-list as a pivot.
15 14 21
9 12 13 21
There are no numbers smaller than the pivot, so write down the pivot.
3
21
QuickSort Example
13 21 15 9 12 3 14 7 6
9 12 3 7 6 13 15 14
3 7 6 9 12 13
The numbers 9, 12, 13 and 21 are now in the correct positions. The next step is to apply the same procedure to any sub-lists containing more than one number.
Look at the first sub-list, and use the first number in the sub-list as a pivot.
15 14 21
9 12 13 213
Then write down the numbers in the sub-list which are greater than the pivot, in the order in which they appear in the original sub-list.
7
7 6
6
21
QuickSort Example
13 21 15 9 12 3 14 7 6
9 12 3 7 6 13 15 14
3 7 6 9 12 13
The numbers 9, 12, 13 and 21 are now in the correct positions. The next step is to apply the same procedure to any sub-lists containing more than one number.
15 14 21
9 12 13 213 7 6
Now look at the second sub-list, and use the first number in the sub-list as a pivot.
14
14
Write down the numbers smaller than the pivot, in the order in which they appear in the original sub-list.
21
QuickSort Example
13 21 15 9 12 3 14 7 6
9 12 3 7 6 13 15 14
3 7 6 9 12 13
The numbers 9, 12, 13 and 21 are now in the correct positions. The next step is to apply the same procedure to any sub-lists containing more than one number.
15 14 21
9 12 13 213 7 6
Now look at the second sub-list, and use the first number in the sub-list as a pivot.
14
14
Then write down the pivot.
In this case there are no numbers greater than the pivot.
15
21
QuickSort Example
13 21 15 9 12 3 14 7 6
9 12 3 7 6 13 15 14
3 7 6 9 12 13
There is now just one sub-list containing more than one number.
15 14 21
9 12 13 213 7 6
Use the first number in the sub-list as a pivot.
14 15
9 12 13 213 14 15
Write down the numbers smaller than the pivot, in the order in which they appear in the original sub-list.
6
6
21
QuickSort Example
13 21 15 9 12 3 14 7 6
9 12 3 7 6 13 15 14
3 7 6 9 12 13
There is now just one sub-list containing more than one number.
15 14 21
9 12 13 213 7 6
Use the first number in the sub-list as a pivot.
14 15
9 12 13 213 14 15
Then write down the pivot.
6
6 7
21
QuickSort Example
13 21 15 9 12 3 14 7 6
9 12 3 7 6 13 15 14
3 7 6 9 12 13
There are now no sub-lists containing more than one number, so the list is sorted.
15 14 21
9 12 13 213 7 6 14 15
9 12 13 213 14 156 7
Quick sort animation
The Simpsons
http://www.youtube.com/watch?v=MtcrEhrt_K0
Lego bubblesort