sorting - university of toronto · sorting objective •given: list of items (numbers, names, etc)....

72
1 Sorting APS105: Computer Fundamentals Jason Anderson Dept. of Electrical and Computer Engineering Faculty of Applied Science and Engineering University of Toronto

Upload: others

Post on 28-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.