Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc)....
Transcript of Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc)....
![Page 1: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/1.jpg)
1
SortingAPS105: Computer Fundamentals
Jason Anderson
Dept. of Electrical and Computer EngineeringFaculty of Applied Science and Engineering
University of Toronto
![Page 2: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/2.jpg)
2
Sorting
Phonebook useless if names were not in alphabetical
(sorted) order
![Page 3: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/3.jpg)
3
Why Sorting?• Sorting used in many computer programs:
– iPod sorts songs based on title or artist.– Facebook sorts friends in alphabetical order.– Facebook gives you the most-recent status.– Excel spreadsheet can sort column by values.– Online dating: sort dating matches based on
how close by they live.– Youtube sorts based on popularity.– .…
![Page 4: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/4.jpg)
4
Sorting Objective• Given: list of items (numbers, names, etc).• Want to put the items sorted order.
• Alpha order• Largest-to-smallest• Smallest-to-largest• Darkest colour to lightest colour
• Sometimes there are so many items, we need computer’s help to do the sorting!
![Page 5: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/5.jpg)
5
What is an Algorithm?
• Set of steps for completing a task.• You have already been designing
algorithms in this class.• You already use algorithms all the time in
your daily life!
![Page 6: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/6.jpg)
6
Algorithm for Baking a Cake• Cream the butter.• Mix the dry ingredients
separately.• Combine the dry and wet
ingredients.• Mix until smooth.• Put into baking pan.• Bake for 30 mins at 350º F.• Is cake done?
– If yes, remove from oven.– If no, bake for another 5 minutes.
![Page 7: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/7.jpg)
7
Algorithm Efficiency• Washer, dryer take 30 mins each.• Have one load of light clothes, one load
dark clothes.Algorithm 1:2 pm : Light clothes into
washer2:30 pm: Light clothes
into dryer3 pm: Darks into washer3:30 pm: Darks into dryer
Algorithm 2: 2 pm: Light clothes into
washer2:30 pm: Light clothes
into dryer AND Darks into washer
3 pm: Darks into dryer
All done at 4 pm! All done at 3:30 pm!
![Page 8: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/8.jpg)
8
Sorting Algorithms• Sequence of steps computer takes for
getting items into the right (sorted) order.• Many different sorting algorithms invented:
– But… they have different efficiencies!• Some take more “steps” to get things into the right
order.• Some work better for different types of items.
• Want fast sorting algorithms:– Sort GTA phonebook with 6 million names.
![Page 9: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/9.jpg)
9
Sorting Algorithms• Research on sorting algorithms:
– Started in 1950s– Still active today
(new algorithm invented in 2004)!• We’ll discuss three classic sorting
approaches today:– Bubble sort– Insertion sort– Quicksort
![Page 10: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/10.jpg)
10
Bubble Sort
• Walk through the list of numbers, comparing two items at a time.– Swap the two items if they’re out of order.
• The biggest item “bubbles” up to the top.• Walk the list of numbers several times until
completely sorted!
![Page 11: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/11.jpg)
11
Bubble SortSort Children from Shortest to Tallest
![Page 12: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/12.jpg)
12
Bubble SortSort Children from Shortest to Tallest
Look at first two children andswap them, if necessary.
![Page 13: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/13.jpg)
13
Bubble SortSort Children from Shortest to Tallest
Look at NEXT two children andswap them, if necessary.
![Page 14: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/14.jpg)
14
Bubble SortSort Children from Shortest to Tallest
Look at NEXT two children andswap them, if necessary.
![Page 15: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/15.jpg)
15
Bubble SortSort Children from Shortest to Tallest
Look at NEXT two children andswap them, if necessary.
![Page 16: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/16.jpg)
16
Bubble SortSort Children from Shortest to Tallest
Look at NEXT two children andswap them, if necessary.
![Page 17: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/17.jpg)
17
Bubble SortSort Children from Shortest to Tallest
Look at LAST two children andswap them, if necessary.
![Page 18: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/18.jpg)
18
Bubble SortSort Children from Shortest to Tallest
Look at LAST two children andswap them, if necessary.
![Page 19: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/19.jpg)
19
Bubble SortSort Children from Shortest to Tallest
Look at FIRST two children andswap them, if necessary.
![Page 20: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/20.jpg)
20
Bubble SortSort Children from Shortest to Tallest
Look at FIRST two children andswap them, if necessary.
![Page 21: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/21.jpg)
21
Bubble SortSort Children from Shortest to Tallest
Look at NEXT two children andswap them, if necessary.
![Page 22: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/22.jpg)
22
Bubble SortSort Children from Shortest to Tallest
Look at NEXT two children andswap them, if necessary.
![Page 23: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/23.jpg)
23
Bubble SortSort Children from Shortest to Tallest
Look at NEXT two children andswap them, if necessary.
![Page 24: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/24.jpg)
24
Bubble SortSort Children from Shortest to Tallest
We don’t need to look at the last TWO, as in each passthe biggest “bubbles up” to the top spot.
![Page 25: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/25.jpg)
25
Bubble SortSort Children from Shortest to Tallest
Look at FIRST two children andswap them, if necessary.
![Page 26: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/26.jpg)
26
Bubble SortSort Children from Shortest to Tallest
Look at NEXT two children andswap them, if necessary.
![Page 27: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/27.jpg)
27
Bubble SortSort Children from Shortest to Tallest
We don’t need to look at these TWO, as in each passthe biggest “bubbles up” to the top spot.
![Page 28: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/28.jpg)
28
Bubble SortSort Children from Shortest to Tallest
We don’t need to look at these TWO, as in each passthe biggest “bubbles up” to the top spot.
Since we didn’t make ANY swaps in this pass,we’re DONE!
![Page 29: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/29.jpg)
29
Bubble Sort
• How many comparisons?• Sort 1000 items:
– 1st pass: 999 comparisons/swaps– 2nd pass: 998 comparisons/swaps– …– 999th pass: 1 comparison/swap
• Sort n items:– (n-1)*(n-2)/2 comparison/swaps
![Page 30: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/30.jpg)
30
Insertion Sort• Like sorting playing cards in hand:
– Draw a first card → card is sorted.– Draw second card → compare with first card.– Draw third card → compare with two cards.– …– …– Draw nth card → compare with n-1 cards.
• Can speed-up using property that hand is kept sorted.
![Page 31: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/31.jpg)
31
Quicksort
• Invented in 1962 by C. Hoare.• Much more efficient than previous
algorithms.– Fewer steps needed to get items into order.
• Still widely used today.• Uses recursion.
![Page 32: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/32.jpg)
32
Recursion
Containssmallerversion ofsame picture
![Page 33: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/33.jpg)
33
Recursion in Computing• Task is broken down into smaller/easier
tasks solved in a similar way.– Ends when we hit a BASE/END case.
![Page 34: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/34.jpg)
34
Recursion in Computing• Task is broken down into smaller/easier
tasks solved in a similar way.– Ends when we hit a BASE/END case.– Invest $1000 at 8% interest.– How much do I have in 3 years?
• Cash(3 years) = Cash(2 years) + 8% x Cash(2 years)• Cash(2 years) = Cash(1 years) + 8% x Cash(1 years)• Cash(1 year) = Cash(0 years) + 8% x Cash(0 years)• Cash(0 years) = $1000.00
• Cash(3 years) = $1259.71!
![Page 35: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/35.jpg)
35
Quicksort Core Action
• Want to sort 10 cards:
6 3 9 0 2 1 7 4 5 8
• Choose a “pivot” card: 6
![Page 36: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/36.jpg)
36
Quicksort Core Action• Walk along the cards and “partition” the cards
into two groups:– Cards smaller than the pivot.– Cards larger than the pivot.
4 3 5 0 2 1 7 8 9 76
Left partition Right partition
Pivot card is where it should be!
Left partition is all less than 6,but is still unsorted!
![Page 37: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/37.jpg)
37
Quicksort Core Action
• Now take the left partition & repeat action!
4 3 5 0 2 1
• Choose a “pivot” card: 4
• Partition the left partition:
2 3 1 0 2 54
Pivot is in its correct place
![Page 38: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/38.jpg)
38
Quicksort Core Action
• Likewise, take the right partition and repeat the process.
8 9 7
• Choose a “pivot” card: 8
• Partition the right partition:
7 9 98
![Page 39: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/39.jpg)
39
Quicksort Overview
Pivot and partition the left and right half.
![Page 40: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/40.jpg)
40
Quicksort Overview
![Page 41: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/41.jpg)
41
Quicksort Overview
![Page 42: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/42.jpg)
42
Quicksort Overview
![Page 43: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/43.jpg)
43
Quicksort Overview
![Page 44: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/44.jpg)
44
Quicksort Overview
Continue until the pieces are so small there is nothing to do! → SORTED
![Page 45: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/45.jpg)
45
Nitty Gritty Details
• How do we do the partitioning?
6 3 9 0 2 1 7 4 5 8
6
LOW HIGH
• Consider two “arrows” LOW and HIGH.
![Page 46: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/46.jpg)
46
Nitty Gritty Details
• How do we do the partitioning?
6 3 9 0 2 1 7 4 5 8
6
LOW HIGH
• Consider two “arrows” LOW and HIGH.• HIGH will march LEFT until it finds a
number less than PIVOT.
![Page 47: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/47.jpg)
47
Nitty Gritty Details
• How do we do the partitioning?
6 3 9 0 2 1 7 4 5 8
6
LOW HIGH
• Consider two “arrows” LOW and HIGH.• HIGH marches LEFT until it finds a number
less than PIVOT.
![Page 48: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/48.jpg)
48
Nitty Gritty Details
• How do we do the partitioning?
6 3 9 0 2 1 7 4 5 8
6
LOW HIGH
• The number at HIGH is moved to the position LOW.
![Page 49: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/49.jpg)
49
Nitty Gritty Details
• How do we do the partitioning?
5 3 9 0 2 1 7 4 5 8
6
LOW HIGH
• The number at HIGH is moved to the position LOW.
![Page 50: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/50.jpg)
50
Nitty Gritty Details
• How do we do the partitioning?
5 3 9 0 2 1 7 4 5 8
6
LOW HIGH
• LOW marches RIGHT until it finds a number greater than PIVOT
![Page 51: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/51.jpg)
51
Nitty Gritty Details
• How do we do the partitioning?
5 3 9 0 2 1 7 4 5 8
6
LOW HIGH
• LOW marches RIGHT until it finds a number greater than PIVOT
![Page 52: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/52.jpg)
52
Nitty Gritty Details
• How do we do the partitioning?
5 3 9 0 2 1 7 4 5 8
6
LOW HIGH
• The number at position LOW is moved to position HIGH.
![Page 53: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/53.jpg)
53
Nitty Gritty Details
• How do we do the partitioning?
5 3 9 0 2 1 7 4 9 8
6
LOW HIGH
• The number at position LOW is moved to position HIGH.
![Page 54: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/54.jpg)
54
Nitty Gritty Details
• How do we do the partitioning?
5 3 9 0 2 1 7 4 9 8
6
LOW HIGH
• HIGH marches left until it finds a number less than PIVOT.
![Page 55: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/55.jpg)
55
Nitty Gritty Details
• How do we do the partitioning?
5 3 9 0 2 1 7 4 9 8
6
LOW HIGH
• HIGH marches left until it finds a number less than PIVOT.
![Page 56: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/56.jpg)
56
Nitty Gritty Details
• How do we do the partitioning?
5 3 9 0 2 1 7 4 9 8
6
LOW HIGH
• The number at position HIGH is moved to position LOW.
![Page 57: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/57.jpg)
57
Nitty Gritty Details
• How do we do the partitioning?
5 3 4 0 2 1 7 4 9 8
6
LOW HIGH
• The number at position HIGH is moved to position LOW.
![Page 58: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/58.jpg)
58
Nitty Gritty Details
• How do we do the partitioning?
5 3 4 0 2 1 7 4 9 8
6
LOW HIGH
• LOW marches right until it finds a number larger than PIVOT.
![Page 59: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/59.jpg)
59
Nitty Gritty Details
• How do we do the partitioning?
5 3 4 0 2 1 7 4 9 8
6
LOW HIGH
• LOW marches right until it finds a number larger than PIVOT.
![Page 60: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/60.jpg)
60
Nitty Gritty Details
• How do we do the partitioning?
5 3 4 0 2 1 7 4 9 8
6
LOW HIGH
• The number at position LOW is moved to position HIGH.
![Page 61: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/61.jpg)
61
Nitty Gritty Details
• How do we do the partitioning?
5 3 4 0 2 1 7 7 9 8
6
LOW HIGH
• The number at position LOW is moved to position HIGH.
![Page 62: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/62.jpg)
62
Nitty Gritty Details
• How do we do the partitioning?
5 3 4 0 2 1 7 7 9 8
6
LOW HIGH
• HIGH marches LEFT until it aligns with LOW.
![Page 63: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/63.jpg)
63
Nitty Gritty Details
• How do we do the partitioning?
5 3 4 0 2 1 7 7 9 8
6
LOW HIGH
• HIGH marches LEFT until it aligns with LOW.
![Page 64: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/64.jpg)
64
Nitty Gritty Details
• How do we do the partitioning?
5 3 4 0 2 1 7 7 9 8
6
LOW HIGH
• The PIVOT is inserted at the position LOW/HIGH.
![Page 65: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/65.jpg)
65
Nitty Gritty Details
• How do we do the partitioning?
5 3 4 0 2 1 7 7 9 8
6
LOW HIGH
• The PIVOT is inserted at the position LOW/HIGH.
6
![Page 66: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/66.jpg)
66
Nitty Gritty Details
• How do we do the partitioning?
5 3 4 0 2 1 7 7 9 8
6
LOW HIGH
• The PIVOT is inserted at the position LOW/HIGH.
6
PARTITIONING COMPLETE!
![Page 67: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/67.jpg)
67
Quicksort • How many comparisons/swaps?
– Sort 1000 items:1000 items
![Page 68: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/68.jpg)
68
Quicksort • How many comparisons/swaps?
– Sort 1000 items:1000 items
~500 items ~500 items
![Page 69: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/69.jpg)
69
Quicksort • How many comparisons/swaps?
– Sort 1000 items:1000 items
~500 items ~500 items
~250 items ~250 items ~250 items ~250 items
![Page 70: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/70.jpg)
70
Quicksort • How many comparisons/swaps?
– Sort 1000 items:1000 items
~500 items ~500 items
~250 items ~250 items ~250 items ~250 items
~1 item ~1 item ~1 item ~1 item ~1 item~1 item. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
~10
leve
ls o
f the
tree
. . . . .
![Page 71: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/71.jpg)
71
Comparing Algorithms
• To sort 1000 times:– Quicksort requires ~10,000 comparisons
(on average).– Bubble sort may require ~500,000
comparisons.– Insertion sort may require ~500,000
comparisons.
![Page 72: Sorting - University of Toronto · Sorting Objective •Given: list of items (numbers, names, etc). •Want to put the items sorted order. •Alpha order •Largest-to-smallest •Smallest-to-largest](https://reader035.fdocuments.net/reader035/viewer/2022080723/5f7c0f18107dfb628073a5d0/html5/thumbnails/72.jpg)
72
Sorting Summary• Sorting: key part of many computer programs!!• Algorithm: set of steps for completing a task.• Different algorithms for same task may have
different efficiencies!• Talked about three sorting algorithms:
– Bubble sort, Insertion Sort and Quicksort• Bubble sort and insertion sort are simple to
build, but need many steps to sort the list.• Quicksort is more complex to build,
sorts list much faster.