Sorting Algorithms What is it: An algorithm that puts the elements of a list in a certain order
Harry Potter & The Sorting Hat Even the magical world of Harry Potter obeys the rules of sorting algorithms Maybe Harry Potter would have been a good computer scientist
Popular Sorting Algorithms 1. Selection sort 2. Quick sort 3. Merge sort 4. Bubble sort 5. Insertion sort
Selection Sort Find the minimum value in the list Swap it with the value of the first position Then look for the next smallest value, and swap it with the value of the 2nd position Repeat, until everything is in order
Selection Sort Sort these numbers in ascending order (small to big) using the selection method 72854
Selection Sort Find the minimum value in the list Swap it with the value of the first position Repeat 27854
Selection Sort
This method is very simple BUT if a list of numbers is really long, you can see how this can take up a lot of time Selection Sort
Pick a pivot (which can be any number on the list) Move the elements either to the left or right of the pivot based on whether the number is larger or smaller than the pivot Pick a new pivot and repeat Quick Sort
Sort these boxes in ascending order using the quick method
Quick Sort Pick a pivot Put all those smaller than it on the left Put all those larger than it on the right Repeat with another pivot until done
Quick Sort
Pick another pivot
Quick Sort YAY!
Divide the unsorted list into n (represents an integer number) sublists, each containing only 1 element Then, repeatedly merge the list back together while sorting the sublists Merge Sort
Sort these letters in alphabetical order using the merge method ALGORITHMS
First, split the list until the sublist only has 1 element Merge Sort ALGORITHMS ALGORITHMS ALGORITHMS ALGORITHMS
Now, merge the sublist while putting them in alphabetical order Merge Sort ALGORITHMS LORIHTMSAG merge and sort: AGLORHIMST
Merge Sort Now each sublist is in alphabetical order, compare the two smallest in each group and merge the lists AGLORHIMST A sorted array which is smaller?
Merge Sort GLORHIMST AG sorted array which is smaller?
Merge Sort LORHIMST AGH sorted array which is smaller?
Merge Sort LORIMST AGHI sorted array which is smaller?
Merge Sort LORMST AGHIL sorted array which is smaller?
Merge Sort ORMST AGHILM sorted array which is smaller?
Merge Sort ORST AGHILMO sorted array which is smaller?
Merge Sort RST AGHILMOR sorted array which is smaller?
Merge Sort ST AGHILMORST SORTED
Start with the first two items in the list Sort them by swapping the items Repeat with the next adjacent pair Bubble Sort
Sort these numbers in ascending order using the bubble method 72854
Bubble Sort Sorted
Pick a random card or number (similar to the pivot on the quick sort) Compare it to the first number If the number is smaller, put it on the card’s left side; if the number is larger, put it on the card’s right side Repeat, until everything is in order Insertion Sort Think of how you sort cards when you hold them
Insertion Sort Sort these numbers in ascending order using the insertion method 72854
7854 Insertion Sort 2
7854 2
7854 2
78542
78425
7842 5
7842 5
7842 5
74258
74258 Sorted!