Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu •...

47
6.006- Introduction to Algorithms Lecture 3 1 Courtesy of MIT Press. Used with permission.

Transcript of Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu •...

Page 1: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

6.006- Introduction to Algorithms

Lecture 3 1

Courtesy of MIT Press. Used with permission.

Page 2: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

   

Menu

• Sorting! – Insertion Sort – Merge Sort

• Solving Recurrences

2

Page 3: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

The problem of sorting

Input: array A[1…n] of numbers.

Output: permutation B[1…n] of A such that B[1] ≤ B[2] ≤ … ≤ B[n] .

e.g. A = [7, 2, 5, 5, 9.6] → B = [2, 5, 5, 7, 9.6]

How can we do it efficiently ?

3

Page 4: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

   

  

   

Why Sorting? • Obvious applications

– Organize an MP3 library – Maintain a telephone directory

• Problems that become easy once items are in sorted order – Find a median, or find closest pairs – Binary search, identify statistical outliers

• Non-obvious applications – Data compression: sorting finds duplicates – Computer graphics: rendering scenes front to back

4

Page 5: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Insertion sort

INSERTION-SORT (A, n) [ A[1 . . n] for j ← 2 to n

insert key A[j] into the (already sorted) sub-array A[1 .. j-1]. by pairwise key-swaps down to its right position

Illustration of iteration j

1 i j n A:

keysorted new location of key

5

Page 6: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Example of insertion sort 8 2 4 9 3 6

6

Page 7: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Example of insertion sort 4 9 3 68 2

7

Page 8: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Example of insertion sort 9 38 2 4

2 8 4 9 3

8

6

6

Page 9: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Example of insertion sort 9 38 2 4

2 8 4 9 3

9

6

6

Page 10: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Example of insertion sort 8 2 4 9 3 6

2 8 4 9 3

2 4 8 9 3 6

10

6

Page 11: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Example of insertion sort 8 2 4 9 3 6

2 8 4 9 3

2 4 8 9 3 6

11

6

Page 12: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Example of insertion sort 8 2 4 9 3 6

2 8 4 9 3

2 4 8 9 3 6

2 4 8 9 3

12

6

6

Page 13: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Example of insertion sort 8 2 4 9 3 6

2 8 4 9 3

2 4 8 9 3 6

2 4 8 9 3

13

6

6

Page 14: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Example of insertion sort 8 2 4 9 3 6

2 8 4 9 3

2 4 8 9 3 6

2 4 8 9 3

2 3 4 8 9 6

14

6

6

Page 15: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Example of insertion sort 8 2 4 9 3 6

2 8 4 9 3

2 4 8 9 3 6

2 4 8 9 3

2 3 4 8 9 6

15

6

6

Page 16: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Example of insertion sort 8 4 9 3 62

2 8 9 3 64

2 4 8 3 69

2 4 8 9 63

2 3 4 8 9 6

2 3 4 6 8 9 done Running time? Θ(n2) because Θ(n2) compares and Θ(n2) swaps

e.g. when input is A = [n, n − 1, n − 2, . . . , 2, 1] 16

Page 17: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Binary Insertion sort

BINARY-INSERTION-SORT (A, n) [ A[1 . . n] for j ← 2 to n

insert key A[j] into the (already sorted) sub-array A[1 .. j-1]. Use binary search to find the right position

Binary search with take Θ(log n) time. However, shifting the elements after insertion will still take Θ(n) time.

Complexity: Θ(n log n) comparisons

Θ

(n2) swaps 17

Page 18: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Meet Merge Sort

MERGE-SORT A[1 . . n] 1. If n = 1, done (nothing to sort).

divide and 2. Otherwise, recursively sort A[ 1 . . n/2 ]conquer and A[ n/2+1 . . n ] .

3. “Merge” the two sorted sub-arrays.

Key subroutine: MERGE 18

Page 19: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Merging two sorted arrays

20 12

13 11

7 9

2 1

19

Page 20: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Merging two sorted arrays

20 12

13 11

7 9

2 1

1

20

Page 21: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Merging two sorted arrays

20 12

13 11

7 9

2 1

1

20 12

13 11

7 9

2

21

Page 22: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Merging two sorted arrays

20 12 20 12

13 11 13 11

7 9 7 9

2 1 2

1 2

22

Page 23: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Merging two sorted arrays

20 12 20 12

13 11 13 11

7 9 7 9

20 12

13 11

7 9

2 1 2

1 2

23

Page 24: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Merging two sorted arrays

20 12

13 11

7 9

2 1

20 12

13 11

7 9

2

20 12

13 11

7 9

1 2 7

24

Page 25: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Merging two sorted arrays

20 12 20 12

13 11 13 11

7 9 7 9

20 12

13 11

7 9

20 12

1113

9

2 1 2

1 2 7

25

Page 26: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Merging two sorted arrays

20 12

13 11

7 9

2 1

20 12

13 11

7 9

2

20 12 20 12

13 11 13 11

7 9 9

1 2 7 9

26

Page 27: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Merging two sorted arrays

20 12

13 11

7 9

2 1

20 12

13 11

7 9

2

20 12 20 12 20 12

13 11 13 11 13 11

7 9 9

1 2 7 9

27

Page 28: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Merging two sorted arrays

20 12

13 11

7 9

2 1

20 12

13 11

7 9

2

7 9

20 12 20 12 20 12

13 11 13 11 13 11

9

1 2 7 9 11

28

Page 29: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Merging two sorted arrays

20 12

13 11

7 9

2 1

20 12

13 11

7 9

2

20 12 20 12 20 12 20

13 11 13 11 1313 11

7 9 9

12

1 2 7 9 11

29

Page 30: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Merging two sorted arrays

20 12

13 11

7 9

2 1

20 12

13 11

7 9

2

20 12 20 12 20 12 20

13 11 13 11 1313 11

7 9 9

12

1 2 7 9 11 12

30

Page 31: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Merging two sorted arrays

20 12

13 11

7 9

2 1

1

20 12

13 11

7 9

2

2

20 12 20 12 20 12 20

13 11 13 11 1313 11

7 9 9

7 9 11 12

12

Time = Θ(n) to merge a total of n elements (linear time).

31

Page 32: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

  

Analyzing merge sort

MERGE-SORT A[1 . . n] 1. If n = 1, done. 2. Recursively sort A[ 1 . . ⎡n/2⎤ ]

and A[ ⎡n/2⎤+1 . . n ] . 3. “Merge” the two sorted lists

Θ(1) if n = 1;T(n) =

2T(n/2) + Θ(n) if n > 1. T(n) = ?

T(n) Θ(1) 2T(n/2)

Θ(n)

32

Page 33: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Recurrence solving Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

33

Page 34: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

T(n)

34

Page 35: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

T(n/2) T(n/2)

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn

35

Page 36: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

cn/2

cn

cn/2

T(n/4) T(n/4) T(n/4) T(n/4)

Recursion tree olve T(n) = 2T(n/2) + cn, where c > 0 is constant. S

36

Page 37: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn

cn/4 cn/4 cn/4 cn/4

cn/2 cn/2

Θ(1)

37

Page 38: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn

cn/4 cn/4 cn/4 cn/4

cn/2 cn/2

Θ(1)

h = 1 + lg n

38

Page 39: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn

cn/4 cn/4 cn/4 cn/4

cn/2 cn/2

Θ(1)

cn

h = 1 + lg n

39

Page 40: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

cn/4 cn/4 cn/4 cn/4

cn/2 cn/2

Θ(1)

cnh = 1 + lg n

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn cn

40

Page 41: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

cn/4 cn/4 cn/4 cn/4

cn/2 cn/2

Θ(1)

cnh = 1 + lg n

cn

cn

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn

41

Page 42: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

cn/4 cn/4 cn/4 cn/4

cn/2 cn/2

Θ(1)

cn

cn

#leaves = n Θ(n)

h = 1 + lg n

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn cn

42

Page 43: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

cn/4 cn/4 cn/4 cn/4

cn/2 cn/2

Θ(1)

cn

cn

#leaves = n Θ(n) Total ?

h = 1 + lg n

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn cn

43

Page 44: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

cn/4 cn/4 cn/4 cn/4

cn/2 cn/2

Θ(1)

= 1 + lg n cn

cn

#leaves = n Θ(n)

Total = Θ(n lg n)

Recursion tree Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn

h

cn

Equal amount of work done at each level 44

Page 45: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

c c c c

c c

Θ(1)

2c

4c

#leaves = n Θ(n) n/2 c

h = 1 + lg n

Tree for different recurrence Solve T(n) = 2T(n/2) + c, where c > 0 is constant.

c c

Note that 1 + ½ + ¼ + … < 2 = ΘTotal (n) All the work done at the leaves

45

Page 46: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

cn2/2h = 1 + lg n

Θ(n)

cn2/16 cn2/16 cn2/16 cn2/16

cn2/4 cn2/4

Θ(1) #leaves = n

cn2/4

Tree for yet another recurrence Solve T(n) = 2T(n/2) + cn2, c > 0 is constant.

cn2 cn2

Note that 1 + ½ + ¼ + … < 2 Total = Θ(n2) All the work done at the root

46

Page 47: Courtesy of MIT Press. Used with permission. Lecture 3 of MIT Press. Used with permission. Menu • Sorting! – Insertion Sort – Merge Sort • Solving Recurrences The problem of

MIT OpenCourseWarehttp://ocw.mit.edu

6.006 Introduction to AlgorithmsFall 2011 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.