Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

Merge Sort

M Shoaib Farooq 1
Merge Sort
-A sort algorithm which splits the items to be sorted into two
groups, recursively sorts each group, and merges them into a
final, sorted sequence. Run time is O(n logn).
- Where n is the number of elements in the group.
Merging
Combining two or more sorted sequences of data into a single
sorted sequence is called merging.
Merge sort divides array A into two parts. Then the no of
comparisons to sort the first half is T(n/2) plus no of comparisons
to sort the second half and the no of comparisons for the merge.
C if n=1
T( n) = T(n/2) + T(n/2) + n if n>1

M Shoaib Farooq 2
Merge Sort
Use Divide and Conquer Approach

 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.
M Shoaib Farooq 3
Merge Sort
Sorting Problem: Sort a sequence or n elements into
non-decreasing order.

 Divide: Divide the n-element sequence to be


into two subsequences of n/2 elements each
sorted
 Conquer: Sort the two subsequences
recursively using merge sort.
 Combine: Merge the two sorted subsequences
to produce the sorted answer.

M Shoaib Farooq 4
M Shoaib Farooq 5
Merge Sort

7 2 ⏐ 94  2 4 7 9

7 ⏐ 2 2 7 9 ⏐ 4 4 9

77 22 99 4


4

M Shoaib Farooq 6
Execution Example

 Partition
7 2 9 4⏐3 8 6 1

7 2 9 4  2 4 7 9 3  1 3 8 6

7 2  2 7 9 4  4 9 3 8  3 6 1
8  1 6

M Shoaib Farooq 7
Execution Example (cont.)

 Recursive call, partition


7 2 9 4⏐3 8 6 1

7 2 ⏐ 94  2 4 7 9 3 8 6 1  1 3 8 6

7 2  2 7 9 4  4 9 3 8  3 8 6 1  1
6

M Shoaib Farooq 8
Execution Example (cont.)

 Recursive call, partition


7 2 9 4⏐3 8 6 1

7 2 ⏐ 94  2 4 7 9 3 8 6 1  1 3 8 6

7⏐2 2 7 9 4  4 9 3 8  3 8 6 1  1
6

M Shoaib Farooq 9
Execution Example (cont.)

 Recursive call, base case


7 2 9 4⏐3 8 6 1

7 2 ⏐ 94  2 4 7 9 3 8 6 1  1 3 8 6

7⏐2 2 7 9 4  4 9 3 8  3 8 6 1  1
6

77

M Shoaib Farooq 10
Execution Example (cont.)

 Recursive call, base case


7 2 9 4⏐3 8 6 1

7 2 ⏐ 94  2 4 7 9 3 8 6 1  1 3 8 6

7⏐2 2 7 9 4 4 9 3 8  3 8 6 1  1
 6

77 22

M Shoaib Farooq 11
Execution Example (cont.)

 Merge
7 2 9 4⏐3 8 6 1

7 2 ⏐ 94  2 4 7 9 3 8 6 1  1 3 8 6

7⏐2 2 7 9 4 4 9 3 8  3 8 6 1  1
 6

77 22

M Shoaib Farooq 12
Execution Example (cont.)

 Recursive call, …, base case, merge


7 2 9 4 ⏐ 38 6 1

7 2 ⏐ 94  2 4 7 9 3 8 6 1  1 3 8 6

7⏐227 9 4  4 9 3 8  3 8 6 1  1 6

77 22 99 44

M Shoaib Farooq 13
Execution Example (cont.)

 Merge
7 2 9 4⏐3 8 6 1

7 2 ⏐ 94  2 4 7 9 3 8 6 1  1 3 8 6

7⏐2 2 7 9 4  4 9 3 8  3 8 6 1  1
6

77 22 99 44

M Shoaib Farooq 14
Execution Example (cont.)

 Recursive call, …, merge, merge


7 2 9 4 ⏐ 38 6 1

7 2 ⏐ 94  2 4 7 9 3 8 6 1  1 3 6 8

7⏐227 9 4  4 9 3 8  3 8 6 1  1 6

77 22 99 44 33 88 66 11

M Shoaib Farooq 15
Execution Example (cont.)

 Merge
7 2 9 4 ⏐ 38 6 1  1 2 3 4 6 7 8 9

7 2 ⏐ 94  2 4 7 9 3 8 6 1  1 3 6 8

7⏐227 9 4  4 9 3 8  3 8 6 1  1 6

77 22 99 44 33 88 66 11

M Shoaib Farooq 16
Merge-Sort (A, p, r)
INPUT: a sequence of n numbers stored in array A
OUTPUT: an ordered sequence of n numbers

MergeSort (A, p, r)
1 if p < r
2 then q  ⎣(p+r)/2⎦
3 MergeSort (A, p, q)
4 MergeSort (A, q+1, r)
5 Merge (A, p, q, r)

Initial Call: MergeSort(A, 1, n)

M Shoaib Farooq 17
Merge
Merge(A, p, q, r)
for i  p to q do
B[i]  A[i]
for i  q+1 to r
do
C[i]  A[i]
jp
kq+1
for i  p to
r do
if j >
q then
A[i] 
C[k] k  k
+1
else if k > r
then
A[i]  B[j]
j  j+1
else if B[j] <=
C[k] then
M Shoaib Farooq
A[i]  18
B[j]
j
Merge Sort
(another Algo)

MergeSort(A, p, r) {
if (p < r) {
q = floor((p + r) / 2);
MergeSort(A, p, q);
MergeSort(A, q+1, r);
Merge(A, p, q, r);
}
}

// Merge() takes two sorted


subarrays of A and
// merges them into a single
sorted subarray of A
// (how long should this
take?)
M Shoaib Farooq 19
Merge
(another Algo)

void Merge(int *A,int p,int q, int r)


{ int n1=q-p+1;
int n2=r-q;
int *L=new int[n1+2]; int *R=new int[n2+2];
for (int i=1; i<=n1; i++)
L[i]=A[p+i-1];
for (int j=1; j<=n2;j++)
R[j]=A[q+j];
L[n1+1]=32000;

R[n2+1]=32000;
i=1; j=1;
for(int k=p;k<=r; k++)
if{ (L[i]<=R[j])
A[k]=L[i]; i=i+1;}
else
{ A[k]=R[j]; j=j+1;}
}
M Shoaib Farooq 20
Merge Sort: Example
 Show MergeSort() running on the array

A = {38, 16,27,39,12,27};

P q q+1 R

M Shoaib Farooq 21
Merge Sort C++ version
#include <iostream.h> void Merge(int *A,int p,int q, int r)
#include <conio.h> { int n1=q-p+1;
void Merge(int *, int,int,int); int n2=r-q;
int *L=new int[n1+2]; int *R=new int[n2+2];
void MergeSort(int*,int,int);
for (int i=1; i<=n1; i++)
void MergeSort(int *a, int p,
L[i]=A[p+i-1];
int r)
for (int j=1; j<=n2;j++)
{if (p<r)
R[j]=A[q+j];
{ int q=(p+r)/2; L[n1+1]=32000;
MergeSort(a,p,q);
MergeSort(a,q+1,r); R[n2+1]=32000;
Merge(a,p,q,r); i=1; j=1;
} for(int
k=p;k<=r; { A[k]=L[i]; i=i+1;}
}
k++) else
void main () { if{ A[k]=R[j] j=j+1;}
int (L[i]<
} ;
a[]={0,38,16,27,39,12,2 =R[j])
7};
MergeSort(a,1,6);
P=3 r=3 (false)
Merge
(pop)
Merge
Merge Sort
Merge Stack Trace
P=6 r=6 (false)
P=2 r=2 (false) (Pop)
(Pop) Merge

P=1 r=1 (false) P=4 r=4


(pop) (false) (pop)
P=1 r=2 q=1 P=4 q=4 r=5
Ms(a,q+1,r)m(a,pq,r) Ms(a,q+1,r)m(a,p,q,r
)
P=1 r=3 q=2 P=4q=5r=6
Ms(a,q+1,r)m(a,p,q,r) Ms(a,q+1,r)m(a,p,q,r
Merge
)
P=1 r=6 q=3
Ms(a,q+1,r)m(a,p,q,r)
Analysis of Merge Sort

Statement Effort
MergeSort(A, p, r) { T(n)
if (p < r) { (1)
q = floor((p + r) / 2); (1)
MergeSort(A, p, q); T(n/2)
MergeSort(A, q+1, r); T(n/2)
Merge(A, p, q, r); (n)
}
}
 T(n) = (1) when n = 1, and
2T(n/2) + (n) when n > 1

M Shoaib Farooq 24
Analysis

-The merge sort algo uses the divide and conquer approach to sort
the n items in A[1..n]. First the array is divided into two sub
arrays, one conatinig n/2 items, and other containing n/2
items. These subarrays are then sorted by recursively calling
the merge sort algorithm-This is the conquer phase of the
algorithm.
-Finally the two sorted sub arrays are combined (I.e. merge
together) to produce the final sorted array.
- Divide phase takes (1) time since all we need to do
is calculate the middle item in the array.
- The conquer phase involves solving two subproblems, one
having a size of n/2 and other a size of n/2.
- Solution of these subproblems can be combined in (n)
M Shoaib Farooq 25
Complexity Of Merge Sort (Master Theorem)

T(n)=2T(n/2) + n
A=2 f(n)=n
b=2
=n which is equal to f(n), Condition
nlogb a nlog22
Case=II are satisfiedof
T(n)= (nlogb a lgn)
= (nlgn) Equal Case II

M. Shoaib Farooq 26 11/23/2010

You might also like