Sorting
CSE 332 (Section 6)
Original slides by: Louis Maliyam • Monty Nitschke • Kevin Pham
Announcements
Comparison Sorts
Recap
(Recap) Insertion Sort
Original input:
3 | 2 | 11 | 12 |
* grey area is the unsorted part
Step 1:
3 | 2 | 11 | 12 |
Step 2:
2 | 3 | 11 | 12 |
Step 3:
11 | 2 | 3 | 12 |
Step 4:
11 | 12 | 2 | 3 |
No swaps needed
(Recap) Selection Sort
Original input:
31 | 32 | 2 | 1 |
* grey area is the unsorted part
Step 1:
1 | 32 | 2 | 31 |
Step 2:
1 | 2 | 32 | 31 |
Step 3:
1 | 2 | 32 | 31 |
Step 4:
1 | 2 | 32 | 31 |
(Recap) Merge Sort
mergeSort(input):
1) mergeSort on left half
2) mergeSort on right half
3) merge left half and right half
(Recap) Quick Sort
* this example SIMPLY chooses the most right element to be a pivot. However, the lecture mentioned about a better way to choose a pivot–median of three.
Note: Sort not fully complete, one more division would occur for [1,2] and [12,15]
quickSort(input):
1) pick pivot
2) partition values into “less than pivot” part and “greater than pivot” part
3) quickSort on “less than pivot” part
4) quickSort on “greater than pivot” part
Problem 2
Sorting Hat
Discuss with neighbors
(Q2) Sorting Hat
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}. (So, in hindsight, the sorting is useless.)
In this case, what is the asymptotic running time of ...
Sorting Algorithm | Asymptotic Runtime | Explanation |
Insertion Sort | O( ) | |
Selection Sort | O( ) | |
Merge Sort | O( ) | |
Quick Sort | O( ) | |
(Q2) Sorting Hat
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}. (So, in hindsight, the sorting is useless.)
In this case, what is the asymptotic running time of ...
Sorting Algorithm | Asymptotic Runtime | Explanation |
Insertion Sort | O(N) | This is the best case runtime of insertion sort as it only requires one pass though the data. |
Selection Sort | O( ) | |
Merge Sort | O( ) | |
Quick Sort | O( ) | |
(Q2) Sorting Hat
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}. (So, in hindsight, the sorting is useless.)
In this case, what is the asymptotic running time of ...
Sorting Algorithm | Asymptotic Runtime | Explanation |
Insertion Sort | O(N) | This is the best case runtime of insertion sort as it only requires one pass though the data. |
Selection Sort | O(N^2) | Selection sort always has N^2 runtime since we have to scan to find the smallest item N times. |
Merge Sort | O( ) | |
Quick Sort | O( ) | |
(Q2) Sorting Hat
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}. (So, in hindsight, the sorting is useless.)
In this case, what is the asymptotic running time of ...
Sorting Algorithm | Asymptotic Runtime | Explanation |
Insertion Sort | O(N) | This is the best case runtime of insertion sort as it only requires one pass though the data. |
Selection Sort | O(N^2) | Selection sort always has N^2 runtime since we have to scan to find the smallest item N times. |
Merge Sort | O(NlogN) | Merge Sort is always NlogN |
Quick Sort | O( ) | |
(Q2) Sorting Hat
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}. (So, in hindsight, the sorting is useless.)
In this case, what is the asymptotic running time of ...
Sorting Algorithm | Asymptotic Runtime | Explanation |
Insertion Sort | O(N) | This is the best case runtime of insertion sort as it only requires one pass though the data. |
Selection Sort | O(N^2) | Selection sort always has N^2 runtime since we have to scan to find the smallest item N times. |
Merge Sort | O(NlogN) | Merge Sort is always NlogN |
Quick Sort | O(N^2) | This is the worst case for Quick Sort, all items will end up on the same side of each partition since all of the elements are the same. |