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

Merge Sort

The document provides an overview of various sorting algorithms, including Insertion Sort, Bubble Sort, Selection Sort, and Merge Sort, detailing their design approaches, in-place sorting capabilities, and running times. It explains the divide-and-conquer strategy used in Merge Sort, which involves dividing the array, recursively sorting the subsequences, and merging them back together. Additionally, it discusses the running time analysis of Merge Sort using recurrence relations and the Master’s Theorem.

Uploaded by

siamshaikh11
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Merge Sort

The document provides an overview of various sorting algorithms, including Insertion Sort, Bubble Sort, Selection Sort, and Merge Sort, detailing their design approaches, in-place sorting capabilities, and running times. It explains the divide-and-conquer strategy used in Merge Sort, which involves dividing the array, recursively sorting the subsequences, and merging them back together. Additionally, it discusses the running time analysis of Merge Sort using recurrence relations and the Master’s Theorem.

Uploaded by

siamshaikh11
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Design and Analysis of

Algorithms
CSE 246
Sorting
• Insertion sort
– Design approach: incrementa
– Sorts in place: lYes
– Best case: Θ(n)
– Worst case: Θ(n2)

• Bubble Sort
– Design approach: incrementa
– Sorts in place: lYes
– Running time: Θ(n2)

2
Sorting
• Selection sort
– Design approach: incrementa
– Sorts in place: lYes
– Running time: Θ(n2)

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

3
Divide-and-Conquer
• Divide the problem into a number of sub-problems
– 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 of the sub-problems


– Obtain the solution for the original problem

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

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)

6
Example – n Power of 2
1 2 3 4 5 6 7 8

Divide 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

7
Example – n Power of 2
1 2 3 4 5 6 7 8

Conquer 1 2 2 3 4 5 6 7
and
Merge 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

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
Divide
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

9
Example – n Not a Power of 2
1 2 3 4 5 6 7 8 9 10 11

Conquer 1 2 2 3 4 4 5 6 6 7 7
and
Merge
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

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]

11
Merging
p q r
• Idea for merging: 1 2 3 4 5 6 7 8

2 4 5 7 1 2 3 6
– 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
A1🡨 A[p, q]
A[p, r]

A2🡨 A[q+1, r]

12
Example: MERGE(A, 9, 12, 16)
p q r

separate
memory

replace in
main
array

13
Example: MERGE(A, 9, 12, 16)

14
Example (cont.)

15
Example (cont.)

16
Example (cont.)

Done!

17
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 n n
L[1 . . n1 + 1] and the next n2 elements1 into R[1 . 2.
n2 + 1] p q

3. L[n1 + 1] ← ∞; R[n2 + 1] ← ∞ L 2 4 5 7 ∞
q+1 r
4. i ← 1; j ← 1
R 1 2 3 6 ∞
5. for k ← p to r
6. do if L[ i ] ≤ R[ j ]
7. then A[k] ← L[ i ]
8. i ←i + 1
9. else A[k] ← R[ j ]
18
Running Time of Merge
(assume last for loop)
• Initialization (copying into temporary arrays):
– Θ(n1 + n2) = Θ(n)
• Adding the elements to the final array:
- n iterations, each taking constant time ⇒ Θ(n)
• Total time for Merge:
– Θ(n)

19
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
20
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
21
Solve the Recurrence
T(n) = c if n =
1
2T(n/2) + cn if n >
1

Use Master’s Theorem:


merge sort complexity
Compare n with f(n) = cn
Case 2: T(n) = Θ(nlgn)

22

You might also like