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:
- Understand what an algorithm is.
- Understand what a sorting algorithm is.
- Understand the mechanics of a few sorting algorithms including Bubble Sort, Merge Sort, and Quick Sort.
- Understand the Runtime Complexity of these algorithms.
- Sorting Algorithms Visualized
- Bubble Sort With Hungarian Folk Dance
- Merge Sort With German Folk Dance
- A Deck of Cards
- Discussion topic: What is an algorithm? Can you give us an example of an algorithm?
- An algorithm is a way for describing automated processes.
- Discussion topic: What is a Sorting Algorithm? Can you give us an example of something that we sort in our day-to-day lives?
- A Sorting Algorithm orders items in a group according to a specificed attribute. ie: Order a roster of students by name, Order cartons of milk by expiration date.
- Discussion topic: Why do we need sorting algorithms in Computer Science?
- Let's do a Card-Sorting activity. Partner up in groups of two or three and order cards from lowest to highest (all of the same suit). Give student groups a deck of cards and give them each a suit of cards to sort.
- After 10 minutes of sorting, let's reflect on how we accomplished it. Did we sort the cards differently from the people in our partner groups? Did we compare a few at a time? Did we look for the lowest first and then go from there? Do you think we could sort in fewer "moves"?
- After this Card-Sorting activity it's clear there are a lot of different ways to sort cards! This translates right to how computer scientists sort Lists. As Computer Scientists we have lots of different algorithms to use that allow us to sort - from Bubble Sort, to Merge Sort, to Quick Sort! Some algorithms aren't as fast as others (Obama knows not to use the Bubble Sort: Watch video), but there are many tradeoffs to different algorithms!
- Why is sorting important? Remember Binary Search last lesson? Well, we need a sorted list to do Binary Searching!
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:
####BogoSort (10 minutes)
Say we want to sort a deck of cards:
- Toss them in the air!
- Pick them up one by one.
- Are they sorted?
- If not, repeat lines 1 - 3
Let's talk about the Complexity of BogoSort. Here's the pseudocode:
def bogo_sort(L):
while not is_sorted(L):
random.shuffle(L)
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?
- Compare consecutive pairs of elements.
- Swap elements in the pair so that the smaller one is first.
- When we reach the end of the list, start over!
- Stop when no more swaps have been made.
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?
def bubble_sort(L):
swap = False
while not swap:
print('bubble sort: ' + str(L))
swap = True
for j in range(1, len(L)):
if L[j-1] > L[j]:
swap = False
temp = L[j]
L[j] = L[j-1]
L[j-1] = temp
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?
- If the smallest element of the list is at the back, Bubble Sort will perform at the absolute worst. Because in each iteration only the largest unsorted element gets put in its proper location. When the smallest element is at the end it will have to be swapped each time through the list and it won't get to the front of the list until all n iterations have occurred.
How does it work?
- First step:
- Extract the smallest element.
- Swap it with the element at index 0.
- Next step:
- In the remaining unsorted sublist extract the smallest element.
- Swap it with the element at index 1.
- At the i'th step, the first i elements of the list are guaranteed to be sorted.
Demonstration video: Watch this video
What's the Complexity of Selection Sort?
def selection_sort(L):
suffixSt = 0
while suffixSt != len(L):
print('selection sort: ' + str(L))
for i in range(suffixSt, len(L)):
if L[i] < L[suffixSt]:
L[suffixSt], L[i] = L[i], L[suffixSt]
suffixSt += 1
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...
- If a list is of length 0 or 1, it's already sorted!
- If a list has more than one element, split it into two lists, and sort each.
- Merge sorted sublists.
- Look at the first element of each, move smaller to the end of the result.
- When one list is empty, just copy the rest of the other list.
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 (left, right) function first.
def merge(left, right):
result = []
i,j = 0,0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
while (i < len(left)):
result.append(left[i])
i += 1
while (j < len(right)):
result.append(right[j])
j += 1
print('merge: ' + str(left) + '&' + str(right) + ' to ' +str(result))
return result
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!
def merge_sort(L):
print('merge sort: ' + str(L))
if len(L) < 2:
return L[:]
else:
middle = len(L)//2
left = merge_sort(L[:middle])
right = merge_sort(L[middle:])
return merge(left, right)
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
- Create this function and a helper function.
- Flesh out the first function by creating variables and then using them in these ways.
- Call helper function.
- Flesh out the helper function by creating these variables and then using them in these ways.
- Return helper result to first function.
- This is why it worked!
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/
- Find out what Radix Sort is.
- Find one more sorting algorithm we didn't mention.
- What is a potential benefit of a Bubble Sort as compared to a Merge Sort?
- Which sort has the potential to be most positively influenced by initial ordering of the list?
- What is the benefit of recursion in sorting? Any drawbacks?
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