5-6 hours
Sorting is important in programming for the same reason it is important in everyday life. It is easier and faster to locate items in a sorted list than unsorted. Sorting algorithms can be used in a program to sort an array for later searching or writing out to an ordered file or report.(by Siddharth Khuntwal)
For example bubble sort, quick sort, and selection sort all take the same input and produce the same output but at different speeds. This is a nice way to show how different approaches can yield more efficient algorithms using approaches such as divide and conquer. On top of that, sorting algorithms don’t require advanced data structures knowledge to understand. If you can understand arrays you can understand sorting algorithm examples.(by cma)
Participants will be able to:
Remember how we can sort cards lots of different ways? Let’s talk about all the ways computers sort Lists.
Let’s start with a Bad Way To Sort:
Say we want to sort a deck of cards:
Let’s talk about the Complexity of BogoSort. Here’s the pseudocode:
The best case?
O(n) where n is the length of the list, because we have to check that the list is sorted.
The worst case?
O(????) because this could NEVER END if we get really unlucky.
Hopefully you can see why we never want to use this algorithm again.
How does it work?
Demonstration video: Watch this Video (useful in slo-mo)
Let’s watch a folk dance interpretation of Bubble Sort: Watch this video, It is also available in the materials section
Let’s break into groups again and try out the sorting algorithm on our own decks of cards!
What’s the Complexity of Bubble Sort?
function bubble_sort(a)
{
let swap;
let n = a.length-1;
let x=a;
do {
swap = false;
for (let i=0; i < n; i++)
{
if (x[i] < x[i+1])
{
let temp = x[i];
x[i] = x[i+1];
x[i+1] = temp;
swap = true;
}
}
n--;
} while (swap);
return x;
}
We have nested loops: the while-loop and the for-loop! The outer loop passes until there are no more swaps. Thus the runtime is O(n^2).
Group question: what state would the list be in for Bubble Sort to have the worst possible performance?
How does it work?
Demonstration video: Watch this video
What’s the Complexity of Selection Sort?
function selection_sort(a){
for( let i = 0; i < a.length; i++ ){
let small = i;
for( let j = i + 1; j < a.length; j++ ){
if( a[j] < a[small]){
small = j;
}
}
if(i !== small){
let temp = a[i];
a[i] = a[small];
a[small] = temp;
}
}
return a;
}
Again, there’s a nested loop, so worst-case runtime is O(n^2)!
Merge Sort is a divide and conquer algorithm. A divide and conquer algorithm works by recursively breaking down a problem into two or more sub-problems of the same or related type, until these become simple enough to be solved directly. The solutions to the sub-problems are then combined to give a solution to the original problem.
What are we dividing and what are we conquering here?
Well, we divide the list into lots of sublists. In fact, we divide them into lists that are of size 1! At that point, we know a list of size 1 is sorted! This isn’t intuitive, right? Let’s dig in…
Let’s review this video: Watch this video, it is also available in the materials section
This will be tough, but let’s try implementing it ourselves!
Let’s try just the merge (a, left, right, middle) function first.
function merge (a, left, right, middle){
n1 = middle - left +1;
n2 = right - middle;
let L = [];
let R = [];
for(let i =0;i<n1;i++){
L[i] = a[left + i ];
}
for(let j=0;j<n2;j++){
R[j] = a[middle+j+1];
}
i = 0;
j = 0 ;
let k = left;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
a[k] = L[i++];
else
a[k] = R[j++];
k++;
}
while (i < n1)
{
a[k] = L[i];
i++;
k++;
}
while (j < n2)
{
a[k] = R[j];
j++;
k++;
}
}
Can we merge this with sample input? What if left = [1,3] and right = [2, 4]?
After an hour, let’s complete the merge_sort function!
function merge_sort (a, left, right){
if(left < right){
let middle = Math.floor((left +right)/2);
merge_sort(a,left,middle);
merge_sort(a,middle+1,right);
merge(a,left,right,middle);
}
}
Let’s test it with this list: testList = [1,3,5,7,2,6,25,18,13]
Spend 3 hours writing steps for yourself on how to go about writing each algorithm: Bubble, Selection, Merge. For example:
Name of Algorithm
If you’re curious to read more there are lots of good resources out there!
MIT Intro to CS Slides on Sorting
Harvey Mudd Lesson Plan on Sorting
https://www.toptal.com/developers/sorting-algorithms/
http://sorting.at/
Thanks to the MIT OpenCourseWare site for Sorting Algorithm Code Examples! https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/lecture-slides-code/lec12_sorting.py