27
Introductory Algorithms Lecture 01: Sorting Algorithms April 8 th , 2009 Jonathan Tse Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

Introductory Algorithms

  • Upload
    hastin

  • View
    37

  • Download
    2

Embed Size (px)

DESCRIPTION

Introductory Algorithms. Lecture 01: Sorting Algorithms April 8 th , 2009 Jonathan Tse. Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up. Sort Playing Cards. Sorting Order Sort into Suits Clubs, Diamonds, Hearts, Spades Sort Suits - PowerPoint PPT Presentation

Citation preview

Page 1: Introductory Algorithms

Introductory Algorithms

Lecture 01: Sorting AlgorithmsApril 8th, 2009Jonathan Tse

Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

Page 2: Introductory Algorithms

2

Sort Playing Cards

• Sorting Order– Sort into Suits• Clubs, Diamonds, Hearts, Spades

– Sort Suits• A 2 3 4 5 6 7 8 9 10 J Q K

• Rules

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

Cards In Hand Cards On Table

Sort Alone Group A Group B

Sort Together Group C Group D

Page 3: Introductory Algorithms

3

Post-Sort Discussion

• What was different about how you sorted?

• Which way was harder/more time consuming?

• Which way took up more physical space?

• Was one way clearly better than another?

• Was one way better in a given situation?

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

Page 4: Introductory Algorithms

4

Post-Sort Discussion

• Moving 1 Card == 1 Processor Action

• Memory Space == Physical Space

• Tradeoff Between:– Number of Processor Actions– Memory Space Used

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

Page 5: Introductory Algorithms

5

Insertion Sort

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

17 2245 18 12Array Space

Working Space

23

Page 6: Introductory Algorithms

6

Insertion Sort

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

17 2245 18 12Array Space

Working Space 2323

Page 7: Introductory Algorithms

7

Insertion Sort

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

17

23 2245 18 12Array Space

Working Space

23 23

Page 8: Introductory Algorithms

8

Insertion Sort

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

2317 22

45

18 12Array Space

Working Space 45

Page 9: Introductory Algorithms

9

Insertion Sort

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

2317 2245

18

12Array Space

Working Space

23 4523

Page 10: Introductory Algorithms

10

Insertion Sort

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

1817 2223 45

12

Array Space

Working Space

17 2318 4517

Page 11: Introductory Algorithms

11

Insertion Sort

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

1712

22

18 23 45Array Space

Working Space

23 4523

Page 12: Introductory Algorithms

12

Insertion Sort

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

1712 4518 22 23

Done!

Page 13: Introductory Algorithms

13

Selection Sort

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

1723 2245 18 12Array Space

Working Space

Page 14: Introductory Algorithms

14

Selection Sort

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

17

23

2245 18

12

Array Space

Working Space

12

Page 15: Introductory Algorithms

15

Selection Sort

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

17

12 2245 18 23Array Space

Working Space 17

Page 16: Introductory Algorithms

16

Selection Sort

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

1712 22

45 18

23Array Space

Working Space

18

Page 17: Introductory Algorithms

17

Selection Sort

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

1712

22

18

45

23Array Space

Working Space

22

Page 18: Introductory Algorithms

18

Selection Sort

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

1712 4518 22

23

Array Space

Working Space 23

Page 19: Introductory Algorithms

19

Selection Sort

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

1712

45

18 22 23

45

Array Space

Working Space

Page 20: Introductory Algorithms

20

Selection Sort

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

1712 4518 22 23

Done!

Page 21: Introductory Algorithms

21

Merge Sort

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

23

17

45

18

12

22

37

36

12

22

37

36

23

17

45

18

12

22

45

18

37

36

23

17

23

17

45

18

12

22

37

36

Array Space Working Space

Page 22: Introductory Algorithms

22

Merge Sort

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

12

17

18

22

23

36

37

45

12

22

36

37

17

12

18

36

1723

17

45

18

12

22

37

36

17

23

18

45

12

22

36

37

17

18

23

45

17

18

23

17

18

Done!

Array Space Working Space

Page 23: Introductory Algorithms

23

0

1

2

3

4

5

6

7

8

9

Radix Sort

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

1723 2245 18 12 3637

23

17

45

18

12 22

37

36

00

10

20

30

40

50

60

70

80

90

23

17

45

1812

22

3736

17 451812 22 373623

Done!

Wor

king

Spa

ceAr

ray

Spac

e

Page 24: Introductory Algorithms

24

Summary of Sorting Algorithms

• Insertion Sort

• Selection Sort

• Merge Sort

• Radix Sort

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

Page 25: Introductory Algorithms

25

Wrap-Up

• Was there anything in particular you noticed about the algorithms?

• Is one better than the other?

• In what situation is one better than the other?

• How do we objectively compare these algorithms?

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

Page 26: Introductory Algorithms

26

Wrap Up

• Is there some comparison metric?

• Can we quantify memory usage?

• What about execution time?

• Does the list to be sorted affect anything?

• Is there a worst case?

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up

Page 27: Introductory Algorithms

27

Next Time

• Standard Metrics for Algorithm Performance

• Worst Case Analysis

4/8/09Overview – Sorting Activity – Post Sort Discussion – Sorting Algorithms – Wrap Up