Sorting: part 1 Barak Obama on sorting, bubble sort, insertion sort, selection sort, merge, merge sort, ... demos, demos, demos …
Sorting, what’s that then? Given a collection of data, get it into order …
Given a collection of data, get it into order … 9 2 6 0 4 3 2 7 5 This is NOT in order
Given a collection of data, get it into order … 0 2 3 4 5 6 7 9 This is IS in order
Given a collection of data, get it into order … 0 2 3 4 5 6 7 9 This is IS in order It’s in ascending order
Sorting is the process of going from this 9 2 6 0 4 3 2 7 5 to that 0 2 3 4 5 6 7 9
Who cares? Sorting, why bother? 9 2 6 0 4 3 2 7 5 You can find things quicker!
9 2 6 0 4 3 2 7 5 You can find things quicker! 0 2 3 4 5 6 7 9 Use “binary search” (or even linear search and stop early)
sorting Some algorithms for sorting bubble sort insertion sort selection sort shell sort gnome sort merge sort quick sort heap sort radix sort bead sort
sorting Some algorithms for sorting bubble sort insertion sort selection sort shell sort gnome sort merge sort quick sort heap sort radix sort bead sort quadratic
sorting Some algorithms for sorting bubble sort insertion sort selection sort shell sort gnome sort merge sort quick sort heap sort radix sort bead sort logarithmic
sorting Some algorithms for sorting bubble sort insertion sort selection sort shell sort gnome sort merge sort quick sort heap sort radix sort bead sort linear
sorting Some algorithms for sorting bubble sort insertion sort selection sort shell sort gnome sort merge sort quick sort heap sort radix sort bead sort Our example
Example sorting 9 2 6 4 3 2 7 5 Bubble Sort
Example sorting 9 2 6 4 3 2 7 5 compare Bubble Sort
Example sorting 2 9 6 4 3 2 7 5 swap Bubble Sort
Example sorting 2 9 6 4 3 2 7 5 compare Bubble Sort
Example sorting 2 6 9 4 3 2 7 5 swap Bubble Sort
Example sorting 2 6 9 4 3 2 7 5 compare Bubble Sort
Example sorting 2 6 9 4 3 2 7 5 swap Bubble Sort
Example sorting 2 6 9 4 3 2 7 5 compare Bubble Sort
Example sorting 2 6 4 9 3 2 7 5 swap Bubble Sort
Example sorting 2 6 4 9 3 2 7 5 compare Bubble Sort
Example sorting 2 6 4 3 9 2 7 5 swap Bubble Sort
Example sorting 2 6 4 3 9 2 7 5 compare Bubble Sort
Example sorting 2 6 4 3 2 9 7 5 swap Bubble Sort
Example sorting 2 6 4 3 2 9 7 5 compare Bubble Sort
Example sorting 2 6 4 3 2 7 9 5 swap Bubble Sort
Example sorting 2 6 4 3 2 7 9 5 compare Bubble Sort
Example sorting 2 6 4 3 2 7 5 9 swap Bubble Sort
Example sorting 2 6 4 3 2 7 5 9 Bubble Sort
Example sorting 2 6 4 3 2 7 5 9 compare Bubble Sort
Example sorting 2 6 4 3 2 7 5 9 compare Bubble Sort
Example sorting 2 6 4 3 2 7 5 9 swap Bubble Sort
Example sorting 2 6 4 3 2 7 5 9 compare Bubble Sort
Example sorting 2 4 6 3 2 7 5 9 swap Bubble Sort
Example sorting 2 4 6 3 2 7 5 9 compare Bubble Sort
Example sorting 2 4 3 6 2 7 5 9 swap Bubble Sort
Example sorting 2 4 3 6 2 7 5 9 compare Bubble Sort
Example sorting 2 4 3 2 6 7 5 9 swap Bubble Sort
Example sorting 2 4 3 2 6 7 5 9 compare Bubble Sort
Example sorting 2 4 3 2 6 7 5 9 compare Bubble Sort
Example sorting 2 4 3 2 6 5 7 9 swap Bubble Sort
Example sorting 2 4 3 2 6 5 7 9 Bubble Sort
Example sorting 2 4 3 2 6 5 7 9 Are you tired yet? Bored? Bubble Sort
Example sorting 2 4 3 2 6 5 7 9 compare Bubble Sort
Example sorting 2 4 3 2 6 5 7 9 swap Bubble Sort
Example sorting 2 4 3 2 6 5 7 9 compare Bubble Sort
Example sorting 2 4 3 2 6 5 7 9 compare Bubble Sort
Example sorting 2 3 4 2 6 5 7 9 swap Bubble Sort
Example sorting 2 3 4 2 6 5 7 9 compare Bubble Sort
Example sorting 2 3 2 4 6 5 7 9 swap Bubble Sort
Example sorting 2 3 2 4 6 5 7 9 compare Bubble Sort
Example sorting 2 3 2 4 6 5 7 9 compare Bubble Sort
Example sorting 2 3 2 4 5 6 7 9 swap Bubble Sort
Example sorting 2 3 2 4 5 6 7 9 Bubble Sort
Example sorting 2 3 2 4 5 6 7 9 compare Bubble Sort
Example sorting 2 3 2 4 5 6 7 9 compare Bubble Sort
Example sorting 2 3 2 4 5 6 7 9 compare Bubble Sort
Example sorting 2 2 3 4 5 6 7 9 swap Bubble Sort
Example sorting 2 2 3 4 5 6 7 9 compare Bubble Sort
Example sorting 2 2 3 4 5 6 7 9 compare Bubble Sort
Example sorting 2 2 3 4 5 6 7 9 Bubble Sort
Example sorting 2 2 3 4 5 6 7 9 compare Bubble Sort
Example sorting 2 2 3 4 5 6 7 9 compare Bubble Sort
Example sorting 2 2 3 4 5 6 7 9 compare Bubble Sort
Example sorting 2 2 3 4 5 6 7 9 compare Bubble Sort
Example sorting 2 2 3 4 5 6 7 9 Bubble Sort
Example sorting 2 2 3 4 5 6 7 9 Phew! Bubble Sort
On an Array sorting Bubble Sort
Example sorting i=8 9 2 6 4 3 2 7 5 Bubble Sort
Example sorting i=8 j=0 9 2 6 4 3 2 7 5 compare Bubble Sort
Example Example sorting i=8 j=0 2 9 6 4 3 2 7 5 swap Bubble Sort
Example Example Example sorting i=8 j=1 2 9 6 4 3 2 7 5 compare Bubble Sort
Example Example Example Example sorting i=8 j=1 2 6 9 4 3 2 7 5 swap Bubble Sort
Example Example Example Example Example sorting i=8 j=2 2 6 9 4 3 2 7 5 compare Bubble Sort
Example Example Example Example Example Example sorting i=8 j=2 2 6 9 4 3 2 7 5 swap Bubble Sort
Example Example Example Example Example Example Example sorting i=8 j=3 2 6 9 4 3 2 7 5 compare Bubble Sort
Example Example Example Example Example Example Example Example sorting i=8 j=3 2 6 4 9 3 2 7 5 swap Bubble Sort
Example Example Example Example Example Example Example Example Example sorting i=8 j=4 2 6 4 9 3 2 7 5 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example sorting i=8 j=4 2 6 4 3 9 2 7 5 swap Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example sorting i=8 j=5 2 6 4 3 9 2 7 5 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example sorting i=8 j=5 2 6 4 3 2 9 7 5 swap Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=6 i=8 2 6 4 3 2 9 7 5 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=6 i=8 2 6 4 3 2 7 9 5 swap Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=7 i=8 2 6 4 3 2 7 9 5 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=7 i=8 2 6 4 3 2 7 5 9 swap Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting 2 6 4 3 2 7 5 9 Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting i=7 j=0 2 6 4 3 2 7 5 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting i=7 j=1 2 6 4 3 2 7 5 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting i=7 j=1 2 6 4 3 2 7 5 9 swap Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting i=7 j=2 2 6 4 3 2 7 5 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting i=7 j=2 2 4 6 3 2 7 5 9 swap Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting i=7 j=3 2 4 6 3 2 7 5 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting i=7 j=3 2 4 3 6 2 7 5 9 swap Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting i=7 j=4 2 4 3 6 2 7 5 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting i=7 j=4 2 4 3 2 6 7 5 9 swap Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting j=5 i=7 2 4 3 2 6 7 5 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting j=6 i=7 2 4 3 2 6 7 5 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting j=6 i=7 2 4 3 2 6 5 7 9 swap Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting 2 4 3 2 6 5 7 9 Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting sorting j=0 i=6 2 4 3 2 6 5 7 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=0 i=6 2 4 3 2 6 5 7 9 swap Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=1 i=6 2 4 3 2 6 5 7 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=2 i=6 2 4 3 2 6 5 7 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=2 i=6 2 3 4 2 6 5 7 9 swap Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=3 i=6 2 3 4 2 6 5 7 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=3 i=6 2 3 2 4 6 5 7 9 swap Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=4 i=6 2 3 2 4 6 5 7 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=5 i=6 2 3 2 4 6 5 7 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=5 i=6 2 3 2 4 5 6 7 9 swap Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting 2 3 2 4 5 6 7 9 Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=0 i=5 2 3 2 4 5 6 7 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=1 i=5 2 3 2 4 5 6 7 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=2 i=5 2 3 2 4 5 6 7 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=2 i=5 2 2 3 4 5 6 7 9 swap Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=3 i=5 2 2 3 4 5 6 7 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=4 i=5 2 2 3 4 5 6 7 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting 2 2 3 4 5 6 7 9 Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=0 i=4 2 2 3 4 5 6 7 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=1 i=4 2 2 3 4 5 6 7 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=2 i=4 2 2 3 4 5 6 7 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting j=3 i=4 2 2 3 4 5 6 7 9 compare Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting 2 2 3 4 5 6 7 9 Bubble Sort
Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example Example sorting 2 2 3 4 5 6 7 9 Bubble Sort
Example sorting 2 2 3 4 5 6 7 9 Bubble Sort
Example sorting 2 2 3 4 5 6 7 9 Phew! Bubble Sort
On an Array sorting Can we visualize this? Bubble Sort
On an Array sorting Can we visualize this? Assume we have a random list of numbers from 0 to n-1, put in an array A Such that A[0] is the 1st number and A[n-1] is the last Bubble Sort
On an Array sorting Can we visualize this? Assume we have a random list of numbers from 0 to n-1, put in an array A Such that A[0] is the 1st number and A[n-1] is the last 9 2 6 4 3 2 7 5 Bubble Sort
On an Array sorting Can we visualize this? Assume we have a random list of numbers from 0 to n-1, put in an array A Such that A[0] is the 1st number and A[n-1] is the last 9 2 6 4 3 5 7 A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] Bubble Sort
On an Array sorting Can we visualize this? Assume we have a random list of numbers from 0 to n-1, put in an array A Such that A[0] is the 1st number and A[n-1] is the last 9 2 6 4 3 5 7 A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] We could plot A[i] on a graph as a point with x/y coordinates i/A[i] Bubble Sort
On an Array sorting Can we visualize this? Assume we have a random list of numbers from 0 to n-1, put in an array A Such that A[0] is the 1st number and A[n-1] is the last 9 2 6 4 3 5 7 A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] We could plot A[i] on a graph as a point with x/y coordinates i/A[i] Do this as the algorithm progress to see how it progresses. Bubble Sort
Ready? On an Array sorting Can we visualize this? Assume we have a random list of numbers from 0 to n-1, put in an array A Such that A[0] is the 1st number and A[n-1] is the last 9 2 6 4 3 5 7 A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] We could plot A[i] on a graph as a point with x/y coordinates i/A[i] Do this as the algorithm progress to see how it progresses. Bubble Sort
On an Array sorting Bubble Sort
sorting Show me more! Bubble Sort
sorting Show me more! different sorting algorithms and relative performance a beauty contest what is the “sound” of sorting? can you show me it by mime? Expressive dance? Bubble Sort
sorting NOTE to self: Bubble sort is a quadratic time algorithm. If I have to sort a data set of N objects the run time will be proportional to N2 To sort 10 numbers will take 100 units of time To sort 100 numbers will take 10,000 units of time Bubble Sort
Barak on Sorting
Links sorting algorithm animation sound of sorting (part 1) sound of sorting (part 2) bubble sort by folk dancing
sorting O(n^2) algorithms bubble sort insertion sort selection sort
On an Array sorting Bubble Sort O(n^2) algorithms bubble sort insertion sort selection sort On an Array sorting Bubble Sort
On an Array sorting Selection Sort O(n^2) algorithms bubble sort insertion sort selection sort On an Array sorting Selection Sort
On a linked list sorting insertion sort O(n^2) algorithms bubble sort selection sort On a linked list sorting insertion sort
merge
merge sorting You have already done this! (QueueSort) Demo with cards?
merge sorting On linked lists
where l1 is of size n1 and l2 is of size n2 merge sorting On linked lists O(n1 + n2) where l1 is of size n1 and l2 is of size n2
sorting merge sort
merge sort sorting msort(L) if (size(L) <= 1) return L return merge(msort(leftHalf(L)),msort(rightHalf(L)))
merge sort sorting msort(L) if (size(L) <= 1) return L return merge(msort(leftHalf(L)),msort(rightHalf(L))) Divide and Conquer divide: divide input data into smaller parts recur: solve the small parts conquer: combine the subproblems once solved (merge)
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting Stopping condition
merge sort sorting Divide list into Left and Right parts
merge sort sorting recurse & merge
a way of “thinking” about merge sort sorting merge sort tree a way of “thinking” about merge sort
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting abbreviated
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting
merge sort sorting n
merge sort sorting n n/2 n/2
merge sort sorting n 2.T(n/2) n/2 n/2
merge sort sorting n 2.T(n/2) n/2 n/2 T(n) is time to cut the data into 2 parts make 2 recursive calls to msort merge the 2 msorted parts and this is linear in n
merge sort sorting n n/2 n/2 n/4 n/4 n/4 n/4
merge sort sorting n n/2 n/2 4.T(n/4) n/4 n/4 n/4 n/4
merge sort sorting n n/2 n/2 n/4 n/4 n/4 n/4 n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8
merge sort sorting n n/2 n/2 n/4 n/4 n/4 n/4 8.T(n/8) n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8
merge sort sorting n n/2 n/2 n/4 n/4 n/4 n/4 8.T(n/8) n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8 This goes on h times where h = log(n) … base 2
merge sort sorting n n/2 n/2 n/4 n/4 n/4 n/4 8.T(n/8) n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8 This goes on h times where h = log(n) … base 2 Therefore O(n) at each level
merge sort sorting n n/2 n/2 n/4 n/4 n/4 n/4 8.T(n/8) n/8 n/8 n/8 n/8 n/8 n/8 n/8 n/8 This goes on h times where h = log(n) … base 2 Therefore O(n) at each level Therefore O(n.log(n))
fin