copyright © the mcgraw-hill companies, inc. permission required for...

31
9/6/07 COT 5407 1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Upload: others

Post on 18-Mar-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 1

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 2: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 2

Sorting

• Input is a list of n items that can be compared.

• Output is an ordered list of those n items.

• Fundamental problem that has received a lot of attention

over the years.

• Used in many applications.

• Scores of different algorithms exist.

• Task: To compare algorithms

– On what bases?

• Time

• Space

• Other

Page 3: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 3

Sorting Algorithms

• Number of Comparisons

• Number of Data Movements

• Additional Space Requirements

Page 4: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 4

Sorting Algorithms

• SelectionSort

• InsertionSort

• BubbleSort

• ShakerSort

• MergeSort

• HeapSort

• QuickSort

• Bucket & Radix Sort

• Counting Sort

Page 5: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 5

SelectionSort

986532After Iteration 5

986532After Iteration 4

968532After Iteration 3

568932After Iteration 2

368952After Iteration 1

362958Initial State

543210Array Position

Page 6: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 6

Psuedocode

• Convention about statements

• Indentation

• Comments

• Parameters -- passed by value not reference

• And/or are short-circuiting

Page 7: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 7

SelectionSort

Page 8: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 8

SelectionSort

Page 9: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 9

SelectionSort: Algorithm Invariants

• iteration k:

– the k smallest items are in correct location

• NEED TO PROVE THE INVARIANT!!

Page 10: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 10

How to prove invariants & correctness

• Initialization: prove it is true at start

• Maintenance: prove it is maintained within iterative control

structures

• Termination: show how to use it to prove correctness

Page 11: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 11

Algorithm Analysis

• Worst-case time complexity

• (Worst-case) space complexity

• Average-case time complexity

Page 12: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 12

SelectionSort

O(n2) time

O(1) space

Page 13: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 13

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

InsertionSort

Page 14: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 14

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 15: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 15

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

O(n2) time

O(1) space

Page 16: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 16

InsertionSort: Algorithm Invariant

• iteration k:

– the first k items are in sorted order.

Page 17: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 17

Figure 8.3Basic action of insertion sort (the shaded part is sorted)

Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Page 18: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 18

Figure 8.4A closer look at the action of insertion sort (the dark shading indicates the

sorted area; the light shading is where the new element was placed).

Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Page 19: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 19

Visualizing Algorithms 1

A

B

Position

Value

Unsorted Sorted

Page 20: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 20

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

O(n2) time

O(1) space

Page 21: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 21

BubbleSort: Algorithm Invariant

• In each pass, every item that does not have

a smaller item after it, is moved as far up in

the list as possible.

• Iteration k:

– k smallest items are in the correct location.

Page 22: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 22

Visualizing Algorithms 2

Position

Value

Unsorted Sorted

Page 23: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 23

Visualizing Comparisons 3

Page 24: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 24

Animation Demos

http://cg.scs.carleton.ca/~morin/misc/sortalg/

Page 25: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 25

Comparing O(n2) Sorting Algorithms

• InsertionSort and SelectionSort (and ShakerSort) are

roughly twice as fast as BubbleSort for small files.

• InsertionSort is the best for very small files.

• O(n2) sorting algorithms are NOT useful for large random

files.

• If comparisons are very expensive, then among the O(n2)

sorting algorithms, insertionsort is best.

• If data movements are very expensive, then among the O(n2)

sorting algorithms, ?? is best.

Page 26: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 26

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 27: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 27

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 28: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 28

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Assumption: Array

A is sorted frompositions p to q

and also from

positions q+1 to r.

Page 29: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 29

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 30: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 30

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 31: Copyright © The McGraw-Hill Companies, Inc. Permission required for …giri/teach/5407/F07/Lec4.pdf · 2007. 9. 6. · 9/6/07 COT 5407 2 Sorting • Input is a list of n items that

9/6/07 COT 5407 31

Problems to think about!

• What is the least number of comparisons you need to sort a

list of 3 elements? 4 elements? 5 elements?

• How to arrange a tennis tournament in order to find the

tournament champion with the least number of matches?

How many tennis matches are needed?