0% found this document useful (0 votes)
10 views

05 Merge Sort

The document discusses the merge sort algorithm. It provides details on how merge sort works by dividing the problem into smaller subproblems, conquering the subproblems recursively by sorting the subarrays, and combining the sorted subarrays. The time complexity of merge sort is analyzed and shown to be O(n log n).
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

05 Merge Sort

The document discusses the merge sort algorithm. It provides details on how merge sort works by dividing the problem into smaller subproblems, conquering the subproblems recursively by sorting the subarrays, and combining the sorted subarrays. The time complexity of merge sort is analyzed and shown to be O(n log n).
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Analysis of Algorithms

Merge Sort

Dr. Ashish Sharma


Sorting
• Insertion sort
– Design approach: incremental
– Sorts in place: Yes
– Best case: (n)
– Worst case: (n2)

• Bubble Sort
– Design approach: incremental
– Sorts in place: Yes
– Running time: (n2)

CS 477/677 - Lecture 5 2
Sorting
• Selection sort
– Design approach: incremental
– Sorts in place: Yes
– Running time: (n2)

• Merge Sort
– Design approach: divide and conquer
– Sorts in place: No
– Running time: Let’s see!!

CS 477/677 - Lecture 5 3
Divide-and-Conquer
• Divide the problem into a number of subproblems
– Similar sub-problems of smaller size

• Conquer the sub-problems


– Solve the sub-problems recursively

– Sub-problem size small enough  solve the problems in


straightforward manner

• Combine the solutions to the sub-problems


– Obtain the solution for the original problem

CS 477/677 - Lecture 5 4
Merge Sort Approach
• To sort an array A[p . . r]:
• Divide
– Divide the n-element sequence to be sorted into two
subsequences of n/2 elements each
• Conquer
– Sort the subsequences recursively using merge sort
– When the size of the sequences is 1 there is nothing
more to do
• Combine
– Merge the two sorted subsequences
CS 477/677 - Lecture 5 5
Merge Sort
p q r
1 2 3 4 5 6 7 8

Alg.: MERGE-SORT(A, p, r) 5 2 4 7 1 3 2 6

if p < r Check for base case

then q ← (p + r)/2 Divide

MERGE-SORT(A, p, q) Conquer

MERGE-SORT(A, q + 1, r) Conquer

MERGE(A, p, q, r) Combine

• Initial call: MERGE-SORT(A, 1, n)

CS 477/677 - Lecture 5 6
Example – n Power of 2
1 2 3 4 5 6 7 8

Example 5 2 4 7 1 3 2 6 q=4

1 2 3 4 5 6 7 8

5 2 4 7 1 3 2 6

1 2 3 4 5 6 7 8

5 2 4 7 1 3 2 6

1 2 3 4 5 6 7 8

5 2 4 7 1 3 2 6

CS 477/677 - Lecture 5 7
Example – n Power of 2
1 2 3 4 5 6 7 8

1 2 2 3 4 5 6 7

1 2 3 4 5 6 7 8

2 4 5 7 1 2 3 6

1 2 3 4 5 6 7 8

2 5 4 7 1 3 2 6

1 2 3 4 5 6 7 8

5 2 4 7 1 3 2 6

CS 477/677 - Lecture 5 8
Example – n Not a Power of 2
1 2 3 4 5 6 7 8 9 10 11

4 7 2 6 1 4 7 3 5 2 6 q=6

1 2 3 4 5 6 7 8 9 10 11

q=3 4 7 2 6 1 4 7 3 5 2 6 q=9

1 2 3 4 5 6 7 8 9 10 11

4 7 2 6 1 4 7 3 5 2 6

1 2 3 4 5 6 7 8 9 10 11

4 7 2 6 1 4 7 3 5 2 6

1 2 4 5 7 8

4 7 6 1 7 3

CS 477/677 - Lecture 5 9
Example – n Not a Power of 2
1 2 3 4 5 6 7 8 9 10 11

1 2 2 3 4 4 5 6 6 7 7

1 2 3 4 5 6 7 8 9 10 11

1 2 4 4 6 7 2 3 5 6 7

1 2 3 4 5 6 7 8 9 10 11

2 4 7 1 4 6 3 5 7 2 6

1 2 3 4 5 6 7 8 9 10 11

4 7 2 1 6 4 3 7 5 2 6

1 2 4 5 7 8

4 7 6 1 7 3

CS 477/677 - Lecture 5 10
Merging
p q r
1 2 3 4 5 6 7 8

2 4 5 7 1 2 3 6

• Input: Array A and indices p, q, r such that


p≤q<r
– Subarrays A[p . . q] and A[q + 1 . . r] are sorted
• Output: One single sorted subarray A[p . . r]

CS 477/677 - Lecture 5 11
Merging

• Idea for merging:


– Two piles of sorted cards
• Choose the smaller of the two top cards
• Remove it and place it in the output pile

– Repeat the process until one pile is empty


– Take the remaining input pile and place it face-down
onto the output pile

CS 477/677 - Lecture 5 12
Merge - Pseudocode
p q r
Alg.: MERGE(A, p, q, r) 1 2 3 4 5 6 7 8

2 4 5 7 1 2 3 6
1. Compute n1 and n2
2. Copy the first n1 elements into n1 n2
L[1 . . n1 + 1] and the next n2 elements into R[1 . . n2 + 1]
3. L[n1 + 1] ← ; R[n2 + 1] ← 
p q

L 2 4 5 7 
4. i ← 1; j ← 1
q+1 r

5. for k ← p to r R 1 2 3 6 
6. do if L[ i ] ≤ R[ j ]
7. then A[k] ← L[ i ]
8. i ←i + 1
9. else A[k] ← R[ j ]
10. j ← j +CS1477/677 - Lecture 5 13
Analyzing Divide-and Conquer
Algorithms
• The recurrence is based on the three steps of
the paradigm:
– T(n) – running time on a problem of size n
– Divide the problem into a subproblems, each of size
n/b: takes D(n)
– Conquer (solve) the subproblems aT(n/b)
– Combine the solutions C(n)
(1) if n ≤ c
T(n) = aT(n/b) + D(n) + C(n) otherwise

CS 477/677 - Lecture 5 14
MERGE-SORT Running Time
• Divide:
– compute q as the average of p and r: D(n) = (1)
• Conquer:
– recursively solve 2 subproblems, each of size n/2
 2T (n/2)
• Combine:
– MERGE on an n-element subarray takes (n) time
 C(n) = (n)
(1) if n =1
T(n) = 2T(n/2) + (n) if n > 1

CS 477/677 - Lecture 5 15
Solve the Recurrence
T(n) = c if n = 1
2T(n/2) + cn if n > 1

Use Master’s Theorem:

Compare n with f(n) = cn


Case 2: T(n) = Θ(nlgn)

CS 477/677 - Lecture 5 16

You might also like