Sorting
CSE 332 – Section 6 (11/2/23)
Slides by James Richie Sulaeman
Announcements
Sorting
Builds a sorted subarray at the front of the original array. Takes the first element of the unsorted subarray and inserts it into the sorted subarray.
Insertion Sort
3 | 2 | 11 | 12 |
Original Input:
3 | 2 | 11 | 12 |
Step 1:
2 | 3 | 11 | 12 |
Step 2:
11 | 2 | 3 | 12 |
Step 3:
11 | 12 | 2 | 3 |
Final output:
unsorted
curr elt
sorted
Builds a sorted subarray at the front of the original array. Takes the smallest element of the unsorted subarray and inserts it at the end of the sorted subarray.
Selection Sort
31 | 32 | 2 | 1 |
Original Input:
1 | 32 | 2 | 31 |
Step 1:
1 | 2 | 32 | 31 |
Step 2:
1 | 2 | 32 | 31 |
Step 3:
1 | 2 | 32 | 31 |
Final output:
sorted
unsorted
Recursively splits an array into two halves, sorts them, and then merges them back together to obtain a sorted array.
Merge Sort
mergeSort(input) -> sorted input:
1. sortedLeft = mergeSort(left half of input)
2. sortedRight = mergeSort(right half of input)
3. return (merged ‘sortedLeft’ and ‘sortedRight’)
4 | 2 | 6 | 0 |
4 | 2 |
4 |
6 | 0 |
2 |
6 |
0 |
2 | 4 |
0 | 6 |
0 | 2 | 4 | 6 |
Recursively partitions an array based on a pivot element, sorts the subarrays on either side of the pivot, and merges them back together to obtain a sorted array.
Quick Sort
quickSort(input) -> void:
1. pick pivot
2. partition input into ‘lessThanPivot’ and
‘greaterThanPivot’ parts
3. quickSort(lessThanPivot)
4. quickSort(greaterThanPivot)
10 | 15 | 1 | 2 |
6 | 12 | 5 | 7 |
1 | 2 | 6 | 5 |
12 | 15 | 10 |
7 |
pivot
12 | 15 |
10 |
1 | 2 |
5 |
6 |
pivot
pivot
Problem 0
Sorting Algorithm | Asymptotic Runtime | Explanation |
Insertion Sort | O(n) | Insertion Sort will traverse the array, but since it is already ‘sorted’, no extra computation is necessary. |
Selection Sort | O(n2) | Selection Sort always has O(n2) runtime since it has to find the smallest item in the unsorted subarray n times. |
Merge Sort | O(n log n) | Merge Sort always has O(n log n) runtime. You can prove this using recurrences! |
Quick Sort | O(n2) | This is the worst case for Quick Sort. Since all the elements are the same, all items will end up on the same side of each partition. |
Suppose we sort an array of numbers, but it turns out every element of the array is the same (e.g. [17, 17, 17, ..., 17]).
What is the asymptotic runtime of the following sorting algorithms?
Problem 0
| |
| |
| |
| |
Suppose we sort an array of numbers, but it turns out every element of the array is the same (e.g. [17, 17, 17, ..., 17]).
What is the asymptotic runtime of the following sorting algorithms?
Problem 0
Sorting Algorithm | Asymptotic Runtime | Explanation |
Insertion Sort | O(n) | Insertion Sort will traverse the array, but since it is already ‘sorted’, no extra computation is necessary |
Selection Sort | O(n2) | Selection Sort always has O(n2) runtime since it has to find the smallest item in the unsorted subarray n times |
Merge Sort | O(n log n) | Merge Sort always has O(n log n) runtime. You can prove this using recurrences! |
Quick Sort | O(n2) | This is the worst case for Quick Sort. Since all the elements are the same, all items will end up on the same side of each partition. |
Non-Comparison Sorts
Bucket Sort
* can be used only when values to be sorted are integers between 1 and K (or any small range)
bucketSort(input):
Count array | |
1 | |
2 | |
3 | |
4 | |
5 | |
K = 5
Input = 5, 1, 3, 4, 3, 2, 1, 1, 5, 4, 5
Bucket Sort
(Recap) Bucket Sort
* can be used only when values to be sorted are integers between 1 and K (or any small range)
bucketSort(input):
Count array | |
1 | 3 |
2 | 1 |
3 | 2 |
4 | 2 |
5 | 3 |
K = 5
Input = 5, 1, 3, 4, 3, 2, 1, 1, 5, 4, 5
Output = 1, 1, 1, 2, 3, 3, 4, 4, 5, 5, 5
Bucket Sort
(Recap) Radix Sort
radixSort(input):
(eg. 10)
Bucket Sort on 1’s digit:
Bucket Sort on 10’s digit:
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| | | | | | | | | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| | | | | | | | | |
B = 10
Input = 478, 537, 9, 721, 3, 38, 143, 67
Radix Sort
(Recap) Radix Sort
radixSort(input):
(eg. 10)
B = 10
Input = 478, 537, 9, 721, 3, 38, 143, 67
Bucket Sort on 1’s digit:
Bucket Sort on 10’s digit:
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 721 | | 3 143 | | | | 537 67 | 478 38 | 9 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| | | | | | | | | |
Radix Sort
(Recap) Radix Sort
radixSort(input):
(eg. 10)
Bucket Sort on 1’s digit:
Bucket Sort on 10’s digit:
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 721 | | 3 143 | | | | 537 67 | 478 38 | 9 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
3 9 | | 721 | 537 38 | 143 | | 67 | 478 | | |
B = 10
Input = 478, 537, 9, 721, 3, 38, 143, 67
Radix Sort
(Recap) Radix Sort
radixSort(input):
(eg. 10)
Bucket Sort on 10’s digit:
Bucket Sort on 100’s digit:
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
3 9 | | 721 | 537 38 | 143 | | 67 | 478 | | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| | | | | | | | | |
B = 10
Input = 478, 537, 9, 721, 3, 38, 143, 67
Radix Sort
(Recap) Radix Sort
= # digits
radixSort(input):
(eg. 10)
Bucket Sort on 10’s digit:
Bucket Sort on 100’s digit:
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
3 9 | | 721 | 537 38 | 143 | | 67 | 478 | | |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
3 9 38 67 | 143 | | | 478 | 537 | | 721 | | |
Output = 3, 9, 38, 67, 143, 478, 537, 721
Radix Sort
Thank You!