Professional Documents
Culture Documents
Introduction To Algorithms: Courtesy of MIT Press. Used With Permission
Introduction To Algorithms: Courtesy of MIT Press. Used With Permission
006- Introduction to
Algorithms
Lecture 3
1
Menu
• Sorting!
– Insertion Sort
– Merge Sort
• Solving Recurrences
2
The problem of sorting
3
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
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:
sorted key
8 2
4 9 3 6
6
Example of insertion sort
8 2 4 9 3 6
7
Example of insertion sort
8 2 4 9 3
6
2 8 4 9 3 6
8
Example of insertion sort
8 2 4 9 3
6
2 8 4 9 3 6
9
Example of insertion sort
8 2
4 9 3 6
2 8
4 9 3 6
2 4 8
9 3 6
10
Example of insertion sort
8 2
4 9 3 6
2 8
4 9 3 6
2 4 8
9 3 6
11
Example of insertion sort
8 2
4 9 3 6
2 8
4 9 3 6
2 4 8
9 3 6
2 4 8 9
3 6
12
Example of insertion sort
8 2
4 9 3 6
2 8
4 9 3 6
2 4 8
9 3 6
2 4 8 9
3 6
13
Example of insertion sort
8 2
4 9 3 6
2 8
4 9 3 6
2 4 8
9 3 6
2 4 8 9
3 6
2 3 4 8 9 6
14
Example of insertion sort
8 2
4 9 3 6
2 8
4 9 3 6
2 4 8
9 3 6
2 4 8 9
3 6
2 3 4 8 9 6
15
Example of insertion sort
8 2
4 9 3 6
2 8
4 9 3 6
2 4 8
9 3 6
2 4 8 9
3 6
2 3 4 8 9 6
2 3 4 6 8 9 done
(n2) swaps
17
Meet Merge Sort
MERGE-SORT A[1 . . n]
1. If n = 1, done (nothing to sort).
divide and
conquer 2. Otherwise, recursively sort A[ 1 . . n/2 ]
and A[ n/2+1 . . n ] .
3. “Merge” the two sorted sub-arrays.
18
Merging two sorted arrays
20 12
13 11
7 9
2 1
19
Merging two sorted arrays
20 12
13 11
7 9
2 1
20
Merging two sorted arrays
20 12 20 12
13 11 13 11
7 9 7 9
2 1 2
21
Merging two sorted arrays
20 12 20 12
13 11 13 11
7 9 7 9
2 1 2
1 2
22
Merging two sorted arrays
20 12 20 12 20 12
13 11 13 11 13 11
7 9 7 9 7 9
2 1 2
1 2
23
Merging two sorted arrays
20 12 20 12 20 12
13 11 13 11 13 11
7 9 7 9 7 9
2 1 2
1 2 7
24
Merging two sorted arrays
20 12 20 12 20 12
20 12
13 11 13 11 13 11
13 11
7 9 7 9 7 9 9
2 1 2
1 2 7
25
Merging two sorted arrays
20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11
7 9 7 9 7 9 9
2 1 2
1 2 7 9
26
Merging two sorted arrays
20 12 20 12 20 12 20 12
20 12
13 11 13 11 13 11 13 11
13 11
7 9 7 9 7 9 9
2 1 2
1 2 7 9
27
Merging two sorted arrays
20 12 20 12 20 12 20 12
20 12
13 11 13 11 13 11 13 11
13 11
7 9 7 9 7 9 9
2 1 2
1 2 7 9 11
28
Merging two sorted arrays
20 12 20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11 13
7 9 7 9 7 9 9
2 1 2
1 2 7 9 11
29
Merging two sorted arrays
20 12 20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11 13
7 9 7 9 7 9 9
2 1 2
1 2 7 9 11 12
30
Merging two sorted arrays
20 12
20 12 20 12 20 12 20 12 20 12
13 11
13 11 13 11 13 11 13 11 13
7 9
7 9 7 9 9
2 1 2
1
2
7 9 11 12
31
Analyzing merge sort
Θ(1) if n = 1;
T(n) =
2T(n/2) + Θ(n) if n > 1.
T(n) = ?
32
Recurrence solving
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
33
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
T(n)
34
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
T(n/2) T(n/2)
35
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/2 cn/2
36
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
cn/2 cn/2
Θ(1)
37
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn
h = 1 + lg n cn/2 cn/2
Θ(1)
38
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
h = 1 + lg n cn/2 cn/2
Θ(1)
39
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
h = 1 + lg n cn/2 cn/2 cn
Θ(1)
40
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
h = 1 + lg n cn/2 cn/2 cn
Θ(1)
41
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
h = 1 + lg n cn/2 cn/2 cn
…
Θ(1) #leaves = n Θ(n)
42
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
h = 1 + lg n cn/2 cn/2 cn
…
Θ(1) #leaves = n Θ(n)
Total ?
43
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
h = 1 + lg n cn/2 cn/2 cn
…
Θ(1) #leaves = n Θ(n)
Total = Θ(n lg n)
Equal amount of work done at each level
44
Tree for different recurrence
c c
c c 2c
h = 1 + lg n
c c c c 4c
…
n/2 c
Θ(1) #leaves = n Θ(n)
Note that 1 + ½ + ¼ + … < 2 Total = Θ(n)
…
Θ(1) #leaves = n Θ(n)
Note that 1 + ½ + ¼ + … < 2 Total = Θ(n2)
All the work done at the root
46
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.