CSE 332 Section 6�Sorting
Original slides by James Richie Sulaeman
Revised by Rubee Zhao & Vicky Ye
Properties: How to choose sorting method?
Comparison-based Sorting
Insertion Sort
Insertion sort 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.
Properties:
Selection Sort
Selection sort builds a sorted subarray at the front of the original array. Takes the first element of the unsorted subarray and swaps it into the correct location at the end of the sorted subarray.
Properties:
Merge Sort
Merge sort recursively splits an array into two halves, sorts them, and then merges them back together to obtain a sorted array.
Properties:
mergeSort(input) -> sorted input:
1. sortedLeft = mergeSort(left half of input)
2. sortedRight = mergeSort(right half of input)
3. return (merged ‘sortedLeft’ and ‘sortedRight’)
Quick Sort
Quick sort 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.
Properties:
quickSort(input) -> void:
1. pick pivot
2. partition input into ‘lessThanPivot’ and
‘greaterThanPivot’ parts
3. quickSort(lessThanPivot)
4. quickSort(greaterThanPivot)
Problem 0
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?
Sorting Algorithm | Asymptotic Runtime | Explanation |
Insertion Sort | | |
Selection Sort | | |
Merge Sort | | |
Quick Sort | | |
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?
Sorting Algorithm | Asymptotic Runtime | Explanation |
Insertion Sort | | |
Merge Sort | | |
Quick Sort | | |
Problem 0: Solution
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(n²) | Selection Sort always has O(n²) 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(n²) | 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. |
Problem 0: Solution
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. |
Merge Sort | O(n log n) | Merge Sort always has O(n log n) runtime. You can prove this using recurrences! |
Quick Sort | O(n²) | 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. |
Problem 1
Problem 1
Given an array of integers as such: {11, 13, 55, 67, 79, 10, 8, 6, 4, 2}. Please answer the following questions (assume all sorts to be done in ascending order):
What is the asymptotic runtime of the following sorting algorithms?
Here, assume we choose the leftmost element as the pivot each time for QS.
Sorting Algorithm | Asymptotic Runtime | Explanation |
Insertion Sort | | |
Merge Sort | | |
Quick Sort | | |
Problem 1
Given an array of integers as such: {11, 13, 55, 67, 79, 10, 8, 6, 4, 2}.
Please answer the following questions (assume all sorts to be done in
ascending order):
What is the asymptotic runtime of the following sorting algorithms?
Here, assume we choose the leftmost element as the pivot each time for QS.
Sorting Algorithm | Asymptotic Runtime | Explanation |
Insertion Sort | O(n²) | The array is not already sorted. At least half of the elements need to iterate through half of the array. So this is the worst case runtime. |
Merge Sort | O(n log n) | Always has O(n log n) runtime. |
Quick Sort | O(n²) | After finishing the sorting of the first pivot (11), we end up sorting the left subarray in worst-case runtime. |
Non-Comparison Sorting
Bucket Sort
Distributes elements into their corresponding buckets. Buckets have an inherent ordering and are merged together in this ordering to produce the sorted array.
Properties:
bucketSort(input) -> sorted input:
1. create array of size B
2. put each element into their corresponding bucket
3. generate sorted array by iterating through the
buckets in order
Original Input:�[51, 11, 31, 41, 32, 2, 12, 13, 52, 42, 53]
Final output:
[11, 12, 13, 2, 31, 32, 41, 42, 51, 52, 53]
Bucket Array | |
1 | 11, 12, 13 |
2 | 2 |
3 | 31, 32 |
4 | 41, 42 |
5 | 51, 52, 53 |
B: 5
Radix Sort - 1’s Digit
Radix Sort repeatedly runs bucket sort on the elements for each significant digit, from least significant to most significant.
Original input: [478, 537, 9, 721, 3, 38, 143, 67], b: 10
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| | | | | | | | | |
Bucket Sort on 1’s Digit
| 721 | | 3, 143 | | | | 537, 67 | 478, 38 | 9 |
radixSort(input) -> sorted input:
1. create array of size b
2. for each significant digit:
3. run bucket sort on the elements using their
corresponding significant digit values
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| | | | | | | | | |
Bucket Sort on 10’s Digit
721 |
3 |
143 |
537 |
67 |
478 |
537, 38 |
3, 9 |
Radix Sort - 10’s Digit
Radix Sort repeatedly runs bucket sort on the elements for each significant digit, from least significant to most significant.
Original input: [478, 537, 9, 721, 3, 38, 143, 67], b: 10
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| | | | | | | | | |
Bucket Sort on 10’s Digit
721 |
3 |
143 |
537 |
537, 38 |
3, 9 |
radixSort(input) -> sorted input:
1. create array of size b
2. for each significant digit:
3. run bucket sort on the elements using their
corresponding significant digit values
67 |
478 |
Radix Sort - 100’s Digit
Radix Sort repeatedly runs bucket sort on the elements for each significant digit, from least significant to most significant.
Original input: [478, 537, 9, 721, 3, 38, 143, 67], b: 10
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| | | | | | 67 | 478 | | |
Bucket Sort on 10’s Digit
721 |
3 |
143 |
537 |
537, 38 |
3, 9 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| | | | | | | | | |
Bucket Sort on 100’s Digit
3 |
3, 9 |
721 |
537 |
3, 9, 38 |
143 |
3, 9, 38, 67 |
478 |
Radix Sort - Final Output
Radix Sort repeatedly runs bucket sort on the elements for each significant digit, from least significant to most significant.
Original input: [478, 537, 9, 721, 3, 38, 143, 67], b: 10
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| | | | | | | | | |
Bucket Sort on 100’s Digit
3 |
3, 9 |
721 |
537 |
3, 9, 38 |
143 |
3, 9, 38, 67 |
478 |
Problem 2
Decision Tree
Decision Tree: Quick Introduction
Decision Tree Example
a<b<c; a<c<b; b<a<c; b<c<a; c<b<a; c<a<b
a<b<c; a<c<b; c<a<b
b<a<c; b<c<a; c<b<a
Ask: is a < b?
a<b<c
a<c<b; c<a<b
b<a<c
b<c<a; c<b<a
a<c<b
c<a<b
b<c<a
c<b<a
Ask: is b<c?
Ask: is a<c?
Ask: is a<c?
Ask: is b<c?
yes
no
yes
no
yes
no
yes
no
yes
no
Decision Tree Example
Thank You!