0% found this document useful (0 votes)
7 views48 pages

2 Merge Sort

Merge sort is a divide-and-conquer algorithm that sorts an array by recursively dividing it into smaller subarrays, sorting those, and then merging them back together. The algorithm has a time complexity of Θ(n log n) and requires additional space of Θ(n). It operates by breaking down the problem into manageable parts, solving them, and then combining the results to achieve a sorted array.

Uploaded by

farjadkhalil917
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views48 pages

2 Merge Sort

Merge sort is a divide-and-conquer algorithm that sorts an array by recursively dividing it into smaller subarrays, sorting those, and then merging them back together. The algorithm has a time complexity of Θ(n log n) and requires additional space of Θ(n). It operates by breaking down the problem into manageable parts, solving them, and then combining the results to achieve a sorted array.

Uploaded by

farjadkhalil917
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

MERGE SORT

Divide and Conquer


1. Base Case, solve the problem directly if it
is small enough

2. Divide the problem into two or more similar


and smaller subproblems

3. Recursively solve the subproblems

4. Combine solutions to the subproblems

2
DIVIDE AND CONQUER

The merge sort algorithm closely follows the


divide-and-conquer paradigm. Intuitively, it
operates as follows.
Divide: Divide the n-element sequence to be sorted
into two subsequences of n=2 elements each.
Conquer: Sort the two subsequences recursively
using merge sort.
Combine: Merge the two sorted subsequences to
produce the sorted answer.
3
DIVIDE AND CONQUER -
SORT
Problem:
Input: A[left..right] – unsorted array of
integers

Output: A[left..right] – sorted in non-decreasing


order

4
Divide and Conquer - Sort
1. Base case
at most one element (left ≥ right), return
2. Divide A into two subarrays: FirstPart, SecondPart
Two Subproblems:
sort the FirstPart
sort the SecondPart
3. Recursively
sort FirstPart
sort SecondPart
4. Combine sorted FirstPart and sorted SecondPart

5
OVERVIEW
Divide and Conquer

Merge Sort

6
MERGE SORT: IDEA

Divide into
A FirstPart SecondPart
two halves
Recursively
sort
FirstPart SecondPart

Merge

A is sorted!

7
MERGE SORT:
ALGORITHM
Merge-Sort (A, left, right)
if left ≥ right return
else
middle ← b(left+right)/2
Recursive Call
Merge-Sort(A, left, middle)
Merge-Sort(A, middle+1, right)
Merge(A, left, middle, right)

8
MERGE-SORT: MERGE
Sorted

A:

merge
Sorted Sorted
FirstPart SecondPart

A:

A[left] A[middle] A[right]


9
MERGE-SORT: MERGE
EXAMPLE

A: 2
5 3
5 7 28
15 8 30
1 4
6 5 14
10 6

L: R:
3 5 15 28 6 10 14 22

Temporary Arrays

10
MERGE-SORT: MERGE
EXAMPLE

A:
3
1 5 15 28 30 6 10 14

k=0

L: R:
3
2 15
3 28
7 30
8 6
1 10
4 14
5 22
6

i=0 j=0

11
MERGE-SORT: MERGE
EXAMPLE

A:
1 2
5 15 28 30 6 10 14

k=1

L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6

i=0 j=1

12
MERGE-SORT: MERGE
EXAMPLE

A:
1 2 3 28 30
15 6 10 14

k=2

L: R:
2 3 7 8 6
1 10
4 14
5 22
6

i=1 j=1

13
MERGE-SORT: MERGE
EXAMPLE

A:
1 2 3 4 6 10 14

k=3

L: R:
2 3 7 8 6
1 10
4 14
5 22
6

i=2 j=1

14
MERGE-SORT: MERGE
EXAMPLE

A:
1 2 3 4 5 6 10 14

k=4

L: R:
2 3 7 8 6
1 10
4 14
5 22
6

i=2 j=2
15
MERGE-SORT: MERGE
EXAMPLE

A:
1 2 3 4 5 6 10 14

k=5

L: R:
2 3 7 8 6
1 10
4 14
5 22
6

i=2 j=3
16
MERGE-SORT: MERGE
EXAMPLE

A:
1 2 3 4 5 6 7 14

k=6

L: R:
2 3 7 8 6
1 10
4 14
5 22
6

i=2 j=4
17
MERGE-SORT: MERGE
EXAMPLE

A:
1 2 3 4 5 6 7 8
14

k=7

L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6

i=3 j=4
18
MERGE-SORT: MERGE
EXAMPLE

A:
1 2 3 4 5 6 7 8

k=8

L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6

i=4 j=4

19
Merge(A, left, middle, right)
1. n1 ← middle – left + 1
2. n2 ← right – middle
3. create array L[n1], R[n2]
4. for i ← 0 to n1-1 do L[i] ← A[left +i]
5. for j ← 0 to n2-1 do R[j] ← A[middle+j]
6. k ← i ← j ← 0
7. while i < n1 & j < n2
8. if L[i] < R[j]
9. A[k++] ← L[i++]
10. else
11. A[k++] ← R[j++]
12. while i < n1
13. A[k++] ← L[i++]
14. while j < n2 n = n1+n2
15. A[k++] ← R[j++]
Space: n
Time : cn for some constant c
20
// L : Left Sub Array , R : Right Sub }
Array , A : Array else
{
merge(L, R, A) A[k] = R[j]
k = k+1
{
j = j+1
nL = length(L) // Size of Left }
Sub Array }
// Adding Remaining elements from left sub
nR = length(R) // Size of Right array to array A
Sub Array while(i<nL)
i=j=k=0 {
A[k] = L[i]
while(i<nL && j<nR) i = i+1
k = k+1
{ }
/* When both i and j are valid
i.e., when both the sub arrays have // Adding Remaining elements from right
elements to insert in A */ sub array to array A
while(j<nR)
if(L[i] <= R[j]) {
A[k] = R[j]
{
j = j+1
A[k] = L[i] k = k+1
}
k = k+1 }
i = i+1
21
MERGE-SORT(A, 0, 7)
Divide
A: 6 2 8 4 33 77 55 1

22
Merge-Sort(A, 0, 7)
Merge-Sort(A, 0, 3) , divide
A: 3 7 5 1

6 2 88 44

23
Merge-Sort(A, 0, 7)
Merge-Sort(A, 0, 1) , divide
A: 3 7 5 1

8 4

6 22

24
Merge-Sort(A, 0, 7)
Merge-Sort(A, 0, 0) , base case
A: 3 7 5 1

8 4

25
Merge-Sort(A, 0, 7)
Merge-Sort(A, 0, 0), return
A: 3 7 5 1

8 4

6 2

26
Merge-Sort(A, 0, 7)
Merge-Sort(A, 1, 1) , base case
A: 3 7 5 1

8 4

27
Merge-Sort(A, 0, 7)
Merge-Sort(A, 1, 1), return
A: 3 7 5 1

8 4

6 2

28
Merge-Sort(A, 0, 7)
Merge(A, 0, 0, 1)
A: 3 7 5 1

8 4

2 6

29
Merge-Sort(A, 0, 7)
Merge-Sort(A, 0, 1), return
A: 3 7 5 1

2 6 8 4

30
Merge-Sort(A, 0, 7)
Merge-Sort(A, 2, 3) , divide
A: 3 7 5 1

2 6

8 44

31
Merge-Sort(A, 0, 7)
Merge-Sort(A, 2, 2), base case
A: 3 7 5 1

2 6

32
Merge-Sort(A, 0, 7)
Merge-Sort(A, 2, 2), return
A: 3 7 5 1

2 6

8 4

33
Merge-Sort(A, 0, 7)
Merge-Sort(A, 3, 3), base case
A:

2 6

34
Merge-Sort(A, 0, 7)
Merge-Sort(A, 3, 3), return
A: 3 7 5 1

2 6

8 4

35
Merge-Sort(A, 0, 7)
Merge(A, 2, 2, 3)
A: 3 7 5 1

2 6

4 8

36
Merge-Sort(A, 0, 7)
Merge-Sort(A, 2, 3), return
A: 3 7 5 1

2 6 4 8

37
Merge-Sort(A, 0, 7)
Merge(A, 0, 1, 3)
A: 3 7 5 1

2 4 6 8

38
Merge-Sort(A, 0, 7)
Merge-Sort(A, 0, 3), return
A: 2 4 6 8 3 7 5 1

39
Merge-Sort(A, 0, 7)
Merge-Sort(A, 4, 7)
A: 2 4 6 8

3 7 5 1

40
Merge-Sort(A, 0, 7)
Merge (A, 4, 5, 7)
A: 2 4 6 8

1 3 5 7

41
Merge-Sort(A, 0, 7)
Merge-Sort(A, 4, 7), return
A: 2 4 6 8 1 3 5 7

42
Merge-Sort(A, 0, 7)
Merge-Sort(A,
Merge(A, 0, 3,0,7)7), done!
A: 1 2 3 4 5 6 7 8

43
Merge-Sort Analysis
n cn

n/2 n/2 2 × cn/2 = cn

log n levels
n/4 n/4 n/4 n/4 4 × cn/4 = cn

n/2 × 2c = cn
2 2 2

Total: cn log n

• Total running time: (nlogn)


• Total Space:  (n)

44
MERGE-SORT SUMMARY
Approach: divide and conquer
Time
 Most of the work is in the merging
 Total time: (n log n)

Space:
 (n), more space than other sorts.

45
COMPLETE BINARY TREE

No. of nodes at level i = 2i

No. of Leaves L = No. of nodes at level H = 2H

Height is the logarithmic function of leaves ; H

= log2L

Total No. of Nodes

 N = (Geometric Series)
46
IF K-ARY
2i = 2K
2H = KH
H = logKL
N = = - 1 / K – 1
So,
1. Height
2. No. of Leaves
3. Base Cost = d*L
4. Internal Cost = IC*H
5. Total Cost = Base Cost + Internal Cost
47
T(n) = 4T(n/2) + cn n>1

T(n) = aT(n/b) + f(n)


Where
a= no. of subproblem
b= division ratio
f(n)=divide time + combine time

48

You might also like