quick sort implementationcmoreno/ece250/quick-sort-complete...quick sort 1 implementation next,...
TRANSCRIPT
![Page 1: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/1.jpg)
Quick Sort
1
Implementation
Next, recall that our goal is to partition all remaining elements based on whether they are smaller than or greater than the pivot
We will find two entries:– One larger than the pivot (staring from the front)– One smaller than the pivot (starting from the back)
which are out of order and then correct the ordering– I.e., swap them
7.6.5
![Page 2: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/2.jpg)
Quick Sort
2
Implementation
Continue doing so until the appropriate entries you find are actually in order
The index to the larger entry we found would be the first large entry in the list (as seen from the left)
Therefore, we could move this entry into the last entry of the list
We can fill this spot with the pivot
7.6.5
![Page 3: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/3.jpg)
Quick Sort
3
Quick Sort Example
First, we examine the first, middle, and last entries of the full list
The span below will indicate which list we are currently sorting
7.6.5
![Page 4: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/4.jpg)
Quick Sort
4
Quick Sort Example
We select 57 to be our pivot– We move 24 into the first location
7.6.5
![Page 5: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/5.jpg)
Quick Sort
5
Quick Sort Example
Starting at the 2nd and 2nd-last locations:– we search forward until we find 70 > 57– we search backward until we find 49 < 57
7.6.5
![Page 6: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/6.jpg)
Quick Sort
6
Quick Sort Example
We swap 70 and 49, placing them in order with respect to eachother
7.6.5
![Page 7: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/7.jpg)
Quick Sort
7
Quick Sort Example
We search forward until we find 97 > 57
We search backward until we find 16 < 57
7.6.5
![Page 8: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/8.jpg)
Quick Sort
8
Quick Sort Example
We swap 16 and 97 which are now in order with respect to each other
7.6.5
![Page 9: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/9.jpg)
Quick Sort
9
Quick Sort Example
We search forward until we find 63 > 57
We search backward until we find 55 < 57
7.6.5
![Page 10: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/10.jpg)
Quick Sort
10
Quick Sort Example
We swap 63 and 55
7.6.5
![Page 11: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/11.jpg)
Quick Sort
11
Quick Sort Example
We search forward until we find 85 > 57
We search backward until we find 36 < 57
7.6.5
![Page 12: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/12.jpg)
Quick Sort
12
Quick Sort Example
We swap 85 and 36, placing them in order with respect to each other
7.6.5
![Page 13: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/13.jpg)
Quick Sort
13
Quick Sort Example
We search forward until we find 68 > 57
We search backward until we find 9 < 57
7.6.5
![Page 14: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/14.jpg)
Quick Sort
14
Quick Sort Example
We swap 68 and 9
7.6.5
![Page 15: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/15.jpg)
Quick Sort
15
Quick Sort Example
We search forward until we find 76 > 57
We search backward until we find 9 < 57– The indices are out of order, so we stop
7.6.5
![Page 16: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/16.jpg)
Quick Sort
16
Quick Sort Example
We move the larger indexed item to the vacancy at the end of the array
We fill the empty location with the pivot, 57
The pivot is now in the correct location
7.6.5
![Page 17: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/17.jpg)
Quick Sort
17
Quick Sort Example
We will now recursively call quick sort on the first half of the list
When we are finished, all entries < 57 will be sorted
7.6.5
![Page 18: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/18.jpg)
Quick Sort
18
Quick Sort Example
We examine the first, middle, and last elements of this sub list
7.6.5
![Page 19: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/19.jpg)
Quick Sort
19
Quick Sort Example
We choose 24 to be our pivot
We move 9 into the first location in this sub-list
7.6.5
![Page 20: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/20.jpg)
Quick Sort
20
Quick Sort Example
We search forward until we find 49 > 24
We search backward until we find 21 < 24
7.6.5
![Page 21: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/21.jpg)
Quick Sort
21
Quick Sort Example
We swap 49 and 21, placing them in order with respect to eachother
7.6.5
![Page 22: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/22.jpg)
Quick Sort
22
We search forward until we find 38 > 24
We search backward until we find 16 < 24
The indices are reversed, so we stop
Quick Sort Example7.6.5
![Page 23: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/23.jpg)
Quick Sort
23
Quick Sort Example
We move 38 to the vacant location and move the pivot 24 into the location previously occupied by 38– 24 is now in the correct location
7.6.5
![Page 24: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/24.jpg)
Quick Sort
24
Quick Sort Example
We will now recursively call quick sort on the left and right halves of those entries which are < 57
7.6.5
![Page 25: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/25.jpg)
Quick Sort
25
Quick Sort Example
The first partition has three entries, so we sort it using insertion sort
7.6.5
![Page 26: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/26.jpg)
Quick Sort
26
Quick Sort Example
The second partition also has only four entries, so again, we use insertion sort
7.6.5
![Page 27: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/27.jpg)
Quick Sort
27
Quick Sort Example
First we examine the first, middle, and last entries of the sub-list
7.6.5
![Page 28: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/28.jpg)
Quick Sort
28
Quick Sort Example
We choose 74 to be our pivot
We move 76 to the vacancy left by 74
7.6.5
![Page 29: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/29.jpg)
Quick Sort
29
Quick Sort Example
We search forward until we find 81 > 74
We search backward until we find 70 < 74
7.6.5
![Page 30: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/30.jpg)
Quick Sort
30
Quick Sort Example
We swap 70 and 84 placing them in order
7.6.5
![Page 31: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/31.jpg)
Quick Sort
31
Quick Sort Example
We search forward until we find 85 > 74
We search backward until we find 61 < 74
7.6.5
![Page 32: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/32.jpg)
Quick Sort
32
Quick Sort Example
We swap 85 and 61 placing them in order
7.6.5
![Page 33: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/33.jpg)
Quick Sort
33
Quick Sort Example
We search forward until we find 79 > 74
We search backward until we find 63 < 74
The indices are reversed, so we stop
7.6.5
![Page 34: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/34.jpg)
Quick Sort
34
Quick Sort Example
We move 79 to the vacant location and move the pivot 74 into the location previously occupied by 79
74 is now in the correct location
7.6.5
![Page 35: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/35.jpg)
Quick Sort
35
Quick Sort Example
We sort the left sub-list first
It has four elements, so we simply use insertion sort
7.6.5
![Page 36: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/36.jpg)
Quick Sort
36
Having sorted the four elements, we focus on the remaining sub-list of seven entries
Quick Sort Example7.6.5
![Page 37: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/37.jpg)
Quick Sort
37
Quick Sort Example
To sort the next sub-list, we examine the first, middle, and last entries
7.6.5
![Page 38: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/38.jpg)
Quick Sort
38
Quick Sort Example
We select 79 as our pivot and move:– 76 into the lowest position– 85 into the highest position
7.6.5
![Page 39: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/39.jpg)
Quick Sort
39
Quick Sort Example
We search forward until we find 85 > 79
We search backward until we find 77 < 79
7.6.5
![Page 40: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/40.jpg)
Quick Sort
40
Quick Sort Example
We swap 85 and 77, placing them in order
7.6.5
![Page 41: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/41.jpg)
Quick Sort
41
Quick Sort Example
We search forward until we find 97 > 79
We search backward until we find 77 < 79
The indices are reversed, so we stop
7.6.5
![Page 42: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/42.jpg)
Quick Sort
42
Quick Sort Example
Finally, we move 97 to the vacant location and copy 79 into the appropriate location– 79 is now in the correct location
7.6.5
![Page 43: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/43.jpg)
Quick Sort
43
Quick Sort Example
This splits the sub-list into two sub-lists of size 2 and 4
We use insertion sort for the first sub-list
7.6.5
![Page 44: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/44.jpg)
Quick Sort
44
Quick Sort Example
We are left with one sub-list with four entries, so again, we use insertion sort
7.6.5
![Page 45: Quick Sort Implementationcmoreno/ece250/quick-sort-complete...Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are](https://reader034.vdocument.in/reader034/viewer/2022051723/5ab7e4e07f8b9a684c8c1574/html5/thumbnails/45.jpg)
Quick Sort
45
Quick Sort Example
Sorting the last sub-list, we arrive at an ordered list
7.6.5