Lecture 18:
Sorting, Part 3
CS 136: Spring 2024
Katie Keith
Record on Zoom
📣 Announcements
📣 Announcements
🎯 Today’s Learning Objectives
📚Readings
Sorting Algorithms
Algorithm | When? | Time Big-O? | (Auxiliary) Space Big-O? |
Selection Sort | Monday Lecture | O(n2) | O(1) |
Insertion Sort | Wednesday Lecture (pseudocode) Lab 5 (implement) | O(n2) | O(1) |
Merge Sort | Friday Lecture |
|
|
Quick Sort | Friday Lecture (pseudocode) Lab 5 extension (implement) | | |
Merge Sort
Invented by John von Neumann in 1945.
Divide & Conquer
Merge sort uses the divide and conquer paradigm, a paradigm that is helpful for many different algorithms in computer science.
Merge Sort: Divide Operation
Recursively divide the unsorted array into two different arrays until each smaller array is a single element (base case).
Divide each array in half
…
Merge Sort: Merge operation
Recursively merge: compare the smallest element in the left array with the smallest element in the right array and repeat.
Do so until there is only one large sorted array remaining.
Two sorted subarrays
7
8
2
9
Merge Sort: Merge operation
Recursively merge: compare the smallest element in the left array with the smallest element in the right array and repeat.
Do so until there is only one large sorted array remaining.
Compare (smallest/
left-most elements)
7
8
2
9
Merge Sort: Merge operation
Recursively merge: compare the smallest element in the left array with the smallest element in the right array and repeat.
Do so until there is only one large sorted array remaining.
7
8
9
2
New merged & sorted array
Merge Sort: Merge operation
Recursively merge: compare the smallest element in the left array with the smallest element in the right array and repeat.
Do so until there is only one large sorted array remaining.
7
8
9
2
New merged & sorted array
Compare (smallest/
left-most elements)
Merge Sort: Merge operation
Recursively merge: compare the smallest element in the left array with the smallest element in the right array and repeat.
Do so until there is only one large sorted array remaining.
8
9
2
7
New merged & sorted array
Merge Sort: Merge operation
Recursively merge: compare the smallest element in the left array with the smallest element in the right array and repeat.
Do so until there is only one large sorted array remaining.
8
9
2
7
New merged & sorted array
Compare (smallest/
left-most elements)
Merge Sort: Merge operation
Recursively merge: compare the smallest element in the left array with the smallest element in the right array and repeat.
Do so until there is only one large sorted array remaining.
8
9
2
7
New merged & sorted array
Compare (smallest/
left-most elements)
Merge Sort: Merge operation
Recursively merge: compare the smallest element in the left array with the smallest element in the right array and repeat.
Do so until there is only one large sorted array remaining.
8
New merged & sorted array
2
7
9
Example
GIF credit: Wikipedia
MergeSort.java
đź’»
int i = lo;
int j = mid+1;
for (int k = lo; k <= hi; k++) {
if (i > mid) a[k] = aux[j++];
else if (j > hi) a[k] = aux[i++];
else if (aux[j]< aux[i]) a[k] = aux[j++];
else a[k] = aux[i++];
}
The chunk of code below is the important part of our merge operation for merge sort.
Use pen & paper to track how the example array a (above) changes as i, j and k change in the code.
27
38
43
3
a
27
38
43
3
aux
lo=0
mid=1
hi=3
Two sorted subarrays from previous steps
lo, mid, and hi designate the boundaries of the sorted subarrays
đź’ˇThink-pair-share
âś…
🎯 Today’s Learning Objectives
Sorting Algorithms
Algorithm | When? | Time Big-O? | (Auxiliary) Space Big-O? |
Selection Sort | Monday Lecture | O(n2) | O(1) |
Insertion Sort | Wednesday Lecture (pseudocode) Lab 5 (implement) | O(n2) | O(1) |
Merge Sort | Friday Lecture |
|
|
Quick Sort | Friday Lecture (pseudocode) Lab 5 extension (implement) | | |
Merge-Sort Time & Space Big-O
Board work
âś…
âś…
âś…
🎯 Today’s Learning Objectives
Sorting Algorithms
Algorithm | When? | Time Big-O? | (Auxiliary) Space Big-O? |
Selection Sort | Monday Lecture | O(n2) | O(1) |
Insertion Sort | Wednesday Lecture (pseudocode) Lab 5 (implement) | O(n2) | O(1) |
Merge Sort | Friday Lecture | O(n log n) | O(n) |
Quick Sort | Friday Lecture (pseudocode) Lab 5 extension (implement) | | |
Quicksort
Invented by Tony Hoare in 1959.
Quicksort overview
Overview:
Divide:
Conquer:
Quick Sort example
Key
Filled black square = pivot element
Red outline = Positions of left pointers (i) and right pointers(j)
Black outline = Elements that are already correctly sorted
Quick Sort pseudocode
Shuffle the array
Recursively:
âś…
âś…
âś…
âś…
🎯 Today’s Learning Objectives
Sorting Algorithms: Conclusion
Algorithm | Time | Auxiliary Space |
Selection Sort | Best = Worst = O(n2) | O(1) |
Insertion Sort | Best = O(n) Worst = O(n2) | O(1) |
Merge Sort | Best = Worst = O(n log n) | O(n) |
Quick Sort | | |
Sorting Algorithms: Conclusion
Algorithm | Time | Auxiliary Space |
Selection Sort | Best = Worst = O(n2) | O(1) |
Insertion Sort | Best = O(n) Worst = O(n2) | O(1) |
Merge Sort | Best = Worst = O(n log n) | O(n) |
Quick Sort | Average = O(n log n) Worst = O(n2) | O(log n) |
Different partitioning strategies can change this
What algorithm for Python’s .sort() method?
Sorting algorithms as music
Start at :28
âś…
âś…
âś…
âś…
âś…
🎯 Today’s Learning Objectives