Download presentation
Presentation is loading. Please wait.
Published byPiers Wade Modified over 9 years ago
1
Sorting Algorithms What is it: An algorithm that puts the elements of a list in a certain order
2
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
3
Popular Sorting Algorithms 1. Selection sort 2. Quick sort 3. Merge sort 4. Bubble sort 5. Insertion sort
4
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
5
Selection Sort Sort these numbers in ascending order (small to big) using the selection method 72854
6
Selection Sort 72854 Find the minimum value in the list Swap it with the value of the first position Repeat 27854
7
72854 27854 24857 24587 24578 Selection Sort
8
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
9
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
10
Sort these boxes in ascending order using the quick method
11
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
12
Quick Sort
13
Pick another pivot
14
Quick Sort YAY!
15
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
16
Sort these letters in alphabetical order using the merge method ALGORITHMS
17
First, split the list until the sublist only has 1 element Merge Sort ALGORITHMS ALGORITHMS ALGORITHMS ALGORITHMS
18
Now, merge the sublist while putting them in alphabetical order Merge Sort ALGORITHMS LORIHTMSAG merge and sort: AGLORHIMST
19
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?
20
Merge Sort GLORHIMST AG sorted array which is smaller?
21
Merge Sort LORHIMST AGH sorted array which is smaller?
22
Merge Sort LORIMST AGHI sorted array which is smaller?
23
Merge Sort LORMST AGHIL sorted array which is smaller?
24
Merge Sort ORMST AGHILM sorted array which is smaller?
25
Merge Sort ORST AGHILMO sorted array which is smaller?
26
Merge Sort RST AGHILMOR sorted array which is smaller?
27
Merge Sort ST AGHILMORST SORTED
28
Start with the first two items in the list Sort them by swapping the items Repeat with the next adjacent pair Bubble Sort
29
Sort these numbers in ascending order using the bubble method 72854
30
Bubble Sort 854 27854 27854 27584 27548 27548 27548 25748 25478 278 24578 72 25478 54 Sorted
31
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
32
Insertion Sort Sort these numbers in ascending order using the insertion method 72854
33
7854 Insertion Sort 2
34
7854 2
35
7854 2
36
78542
37
7 8 542
38
78425
39
7842 5
40
7842 5
41
7842 5
42
74258
43
7 4 258
44
7 4 258
45
7 4 258
46
7 4 258
47
74258 Sorted!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.