Vous êtes sur la page 1sur 52

PRESENTATION ON

MERGE SORT
Submitted to -
Dr. Vinay Pathak

Submitted by:
Abhishek Singh
SR. NO: 96/07
0704510002
Topics Covered
1. The Divide & Conquer approach

2. Merge sort
3. Designing Merge Sort Algorithm

4. Code

5. Analysing divide & conquer approach


6. Analysis of merge sort
The Divide & Conquer Approach
• Many useful algorithms are recursive in structure:
to solve a given problem, the relatey call
themselves recursively one or more times to deal
with closely related subproblems. These
algorithms follow a divide & conquer approach:
They break the problem into several sub-problems
similar to original one, smaller in size, solve them
recursively & combine the solutions to create a solution
to the original problem.
• The divide and conquer paradigm involves three steps
at each level of recursion:

1. Divide the problem into a number of subprolems.


2. Conquer the subproblems by solving them recursively.
e
if the subproblem sizes are small enough, however, just

solve the subprograms in a straightforward manner.


3. Combine the solutions to the subproblems into the
solution for the original problem.

The merge sort algorithm closely follows the divide and


Conquer paradigm.
Mergesort
•Mergesort (divide-and-conquer)
– Divide array into two halves.

A L G O R I T H M S

A L G O R I T H M S divide
Mergesort
•Mergesort (divide-and-conquer)
– Divide array into two halves.
– Recursively sort each half.

A L G O R I T H M S

A L G O R I T H M S divide

A G L O R H I M S T sort
Mergesort
•Mergesort (divide-and-conquer)
– Divide array into two halves.
– Recursively sort each half.
– Merge two halves to make sorted whole.
A L G O R I T H M S

A L G O R I T H M S divide

A G L O R H I M S T sort

A G H I L M O R S T merge
Merging
•Merge.
– Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.

smallest smallest

A G L O R H I M S T

A auxiliary array
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.

smallest smallest

A G L O R H I M S T

A G auxiliary array
Merging
•Merge.
– Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.

smallest smallest

A G L O R H I M S T

A G H auxiliary array
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.

smallest smallest

A G L O R H I M S T

A G H I auxiliary array
Merging
•Merge.
– Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.

smallest smallest

A G L O R H I M S T

A G H I L auxiliary array
Merging
•Merge.
– Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.

smallest smallest

A G L O R H I M S T

A G H I L M auxiliary array
Merging
•Merge.
– Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.
–.
smallest smallest

A G L O R H I M S T

A G H I L M O auxiliary array
Merging
• Merge.
– Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done.

smallest smallest

A G L O R H I M S T

A G H I L M O R auxiliary array
Merging
.Merge.
– Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done
first half
exhausted smallest

A G L O R H I M S T

A G H I L M O R S auxiliary array
Merging
.Merge.
– Keep track of smallest element in each sorted half.
– Insert smallest of two elements into auxiliary array.
– Repeat until done
first half
exhausted smallest

A G L O R H I M S T

A G H I L M O R S T auxiliary array
Designing Merge Sort Algorithm

• The merge sort algorithm closely follows the divide and


conquer paradigm. It operates as follows.
1. Divide : Divide the n elements sequence to be sorted into
two subsequences of n/2 elements each.

2. Conquer : Sort the two subsequences recursively using


merge sort.

3. Combine : Merge the two sorted subsequences to


produce the sorted answer.
MERGE(A,p,q,r)
1. n1 <- q-p+1
2. n2 <- r-q
3. Create arrays L[1….n1+1] and R[1….n2+1]
4. for i <- 1 to n1
5. do L[i] <- A[p+i-1]
6. for j <- 1 to n2
7. do R[j] <- A[q+j]
8. L[n1+1] <- *
9. R[n2+1] <- *
10. i <- 1
11. J <- 1
12. for k <- p to r
13.do if L[i] <= R[j]
14.then A[k] <- L[i]
15.i <- i+1
16.else A[k] <- R[j]
17.j <- j+1
The following pseudocode implements the above idea, but with
An additional twist that avoids having to check whether either
Pile is empty in each basic step. The idea is to put on the
Bottom of each pile a sentinel card, which contains a special
Value that we use to specify our code. Here we use * as
The sentinel value, so that whenever a card with * is
Exposed, it cannot be smaller card unless both piles have
Their sentinel card exposed. But once that happens, all the non
Sentinel cards have already been placed onto the output pile.
As we know in advance that exactly r-p+1 cards will be placed
Onto the output pile.
Implementation of merge procedure.

lets take an array A of which p,q,r are indices


numbering elements of array such that p<=q<r.
Procedure assumes that subarrays A[p….q] &
A[q+1….r]
Are in sorted order. Trough an example we will
see that
Previous algorithm merges them to form a single
sorted
Subarray that replaces the current subarray
A[p….r].
p Q Q+1 R
15
9 10 11 12 13 14 16

2 4 5 7 1 2 3 6
1 4
2 3 4 5 1 2 3 5

R
L

MERGE(A,p,q,r)
Look at the indices
1. n1 <- q-p+1
p->9
2. N2 <- r-q
q->12
3. Create arrays L[1..n1+1]
q+1->13
& R[1..n2+1]
r->16

Length of array is n1+1, to store the sentinal value


Represented by infinity.
p Q Q+1 R
15
9 10 11 12 13 14 16
(A)

2 4 5 7 1 2 3 6
1 4
2 3 4 5 1 2 3 5

2 4 5 7 * R1 2 3 6 *
L
j
i

Look at the L & R arrays. MERGE(A,p,q,r)


Already sorted. 4. for i <- 1 to n1
Our job is to merge them in 5. do L[i] <- A[p+i-1]
Sorted order in a single array A. 6. for j <- 1 to n2
7. do R[j] <- A[q+j]
8. L[n1+1] <- *
9. R[n2+1] <- *
10. i <- 1
11. j <- 1
p Q Q+1 R
(b)
15
9 10 11 12 13 14 16

1 4 5 7 1 2 3 6
k

1 4
2 3 4 5 1 2 3 5

2 4 5 7 * R1 2 3 6 *
L
j
i
MERGE(A,p,q,r)
12. For k <- p to r
13. do if L[i] <= R[j]
14. then A[k] <- L[i]
15. i <- i+1
16. else A[k] <- R[j]
17. j <- j+1
p Q Q+1 R
15
(c) 9 10 11 12 13 14 16

1 2 5 7 1 2 3 6
k

1 4
2 3 4 5 1 2 3 5

2 4 5 7 * R1 2 3 6 *
L
j
i

16
(d) 9 10 11 12 13 14 15

1 2 2 7 1 2 3 6
k

1 2 3 4 5 1 2 3 4 5

R
L 2 4 5 7 * 1 2 3 6 *
i j
p Q Q+1 R
(e) 15
9 10 11 12 13 14 16

1 2 2 3 1 2 3 6
k

1 4
2 3 4 5 1 2 3 5

2 4 5 7 * R1 2 3 6 *
L
j
i

16
(f) 9 10 11 12 13 14 15

1 2 2 3 4 2 3 6
k

1 2 3 4 5 1 2 3 4 5

2 4 5 7 * R
1 2 3 6 *
L

i
j
p Q Q+1 R
15
(g) 9 10 11 12 13 14 16

1 2 2 3 4 5 3 6
k

1 4
2 3 4 5 1 2 3 5

2 4 5 7 * R1 2 3 6 *
L
i j

9
(h) 10 11 12 13 14 15 16

1 2 2 3 4 5 6 6
k

1 2 3 4 5 1 2 3 4 5

2 4 5 7 * R
1 2 3 6 *
L

i j
p Q Q+1 R
15
(i) 9 10 11 12 13 14 16

1 2 2 3 4 5 6 7 k

1 4
2 3 4 5 1 2 3 5

2 4 5 7 * R1 2 3 6 *
L
i j

Finally we have merged the arrays L & R in sorted order in a


single array A.
MERGE SORT PROCEDURE

We can now use the MERGE procedure as a subroutine in


The merge sort algorithm. The procedure MERGE-
SORT(A,p,r) sorts the elements in the subarray A[p..r].
If p=>r, the subarray has atmost one element and is
therefore already sorted. Otherwise the divide step
simply computes an index q that partitions A[p..r] into
two subarrays:
A[p….r]

A[p….q] A[q+1….r]
containing
n/2 elements
MERGE-SORT(A,p,r)
1. if p<r
2. then q <- [(p+r)/2]
3. MERGE-SORT(A,p,q)
4. MERGE-SORT(A,q+1,r)
5. MERGE(A,p,q,r)

To sort the entire sequence A = ( A[1] ,A[2] ,……….A[n]),


we make the
Initial call MERGE-SORT(A,1,length[A]), where length[A]=n
Execution Example
• Partition
7 2 9 4 3 8 6 1 → 1 2 3 4 6 7 8 9

7 2 9 4 → 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 3 →3 8 →8 6 →6 1 →1
Execution Example (cont.)

• Recursive call, partition


7 2 9 4 3 8 6 1 → 1 2 3 4 6 7 8 9

7 2 9 4 → 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 3 →3 8 →8 6 →6 1 →1
Execution Example (cont.)
• Recursive call, partition
7 2 9 4 3 8 6 1 → 1 2 3 4 6 7 8 9

7 2 9 4 → 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 3 →3 8 →8 6 →6 1 →1
Execution Example (cont.)
• Recursive call, base case
7 2 9 4 3 8 6 1 → 1 2 3 4 6 7 8 9

7 2 9 4 → 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 3 →3 8 →8 6 →6 1 →1
Execution Example (cont.)
• Recursive call, base case
7 2 9 4 3 8 6 1 → 1 2 3 4 6 7 8 9

7 2 9 4 → 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 3 →3 8 →8 6 →6 1 →1
Execution Example (cont.)
• Merge
7 2 9 4 3 8 6 1 → 1 2 3 4 6 7 8 9

7 2 9 4 → 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 3 →3 8 →8 6 →6 1 →1
Execution Example (cont.)
• Recursive call, …, base case, merge
7 2 9 4 3 8 6 1 → 1 2 3 4 6 7 8 9

7 2 9 4 → 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 3 →3 8 →8 6 →6 1 →1
Execution Example (cont.)
• Merge
7 2 9 4 3 8 6 1 → 1 2 3 4 6 7 8 9

7 2 9 4 → 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 3 →3 8 →8 6 →6 1 →1
Execution Example (cont.)
• Recursive call, …, merge, merge
7 2 9 4 3 8 6 1 → 1 2 3 4 6 7 8 9

7 2 9 4 → 2 4 7 9 3 8 6 1 → 1 3 6 8

7 2 → 2 7 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
Execution Example (cont.)
• Merge
7 2 9 4 3 8 6 1 → 1 2 3 4 6 7 8 9

7 2 9 4 → 2 4 7 9 3 8 6 1 → 1 3 6 8

7 2 → 2 7 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
Merge-sort Analysis

• Assume that keys are random, uniformly


distributed.
• What is best case running time?
– Recursion:
1. Partition splits array in two sub-arrays of size
n/2
1. Sort each sub-array

– Depth of recursion tree? O(log2n)


– Number of accesses in partition? O(n)
Best Case

• Partition splits the array evenly


T (n) = 2T (n / 2) + Θ(n)
Merge-sort Analysis

• Assume that keys are random,


uniformly distributed.
• Running time: O(n log2n)
Analysis of Merge-Sort
• The height h of the merge-sort tree is O(log n)
– at each recursive call we divide in half the sequence,
• The overall amount or work done at the nodes of depth i is O(n)
– we partition and merge 2i sequences of size n/2i
– we make 2i+1 recursive calls
• Thus, the total running time of merge-sort is O(n log n)

depth #seqs size


0 1 n

1 2 n/2

i 2i n/2i

… … …
Code for MERGE-SORT
• #include<stdio.h>
• #include<stdlib.h>
• #include<conio.h>
• #include<time.h>
• #define MAX 10000

• void merge_sort(int [],long,long);


• void merge(int [],long,long,long);

• void main()
• {
• int a[MAX];
• long n,i;
• clock_t start,end;
• clrscr();
• randomize();

• printf("\n Enter no. of elements :");
• scanf("%ld",&n);


• for(i=0;i<n;i++)
• a[i]=random(10000);
• printf("\n Unsorted data ->\n");
• for(i=0;i<n;i++)
• printf("%d\t",a[i]);

• start=clock();
• merge_sort(a,0,n-1);

• printf("\n Sorted data ->\n");


• for(i=0;i<n;i++)
• printf("%d\t",a[i]);
• end=clock();
• printf("\nThe time elapsed(in sec): %f\n", (end - start)/CLK_TCK);

• getch();
• }
• void merge_sort(int a[],long i,long j)
• {
• long k;
• if(i<j)
• {
• k=(i+j)/2;
• merge_sort(a,i,k);
• merge_sort(a,k+1,j);
• merge(a,i,k,j);
• }
• }

• void merge(int a[],long l,long m,long u)


• {
• int c[MAX];
• long i,j,k;

• i=l;
• j=m+1;
• k=0;
• while(i<=m && j<=u)
• {

• if(a[i] < a[j])
• {
• c[k]=a[i];
• k++;i++;
• }
• else
• {
• c[k]=a[j];
• k++;j++;
• }
• }
• while(i<=m)
• {
• c[k]=a[i];
• i++;k++;
• }
• while(j<=u)
• {
• c[k]=a[j];
• k++;j++;
• }
• for(i=l,j=0;i<=u;i++,j++)
• a[i]=c[j];

• }
OUTPUTS
Enter no. of elements :10

Unsorted data ->


852 7203 8009 3791 9857 9386
3427 3275 3368 4027

Press any key to start sorting...

Sorted data ->


852 3275 3368 3427 3791 4027
7203 8009 9386 9857

The time elapsed(in sec): 0.000000


Enter no. of elements :50

Unsorted data ->


1843 359 2429 9844 5198 5325 5978 8434 8110
3569
4169 2083 9749 9028 3403 973 9662 3472 7257
1133
3403 7949 8645 7144 6462 4169 8872 254 4936
3021
4529 1587 6366 2402 8140 5145 9201 1552 3539
269
4301 4080 1871 8278 6933 6871 9187 2939 2695
7053

Press any key to start sorting...

Sorted data ->


254 269 359 973 1133 1552 1587 1843 1871
2083
2402 2429 2695 2939 3021 3403 3403 3472 3539
Output for MERGE-SORT algo.

No. of 100 1000 5000 7000 10000


elements

Time in 0.054945 0.109890 0.109890


seconds 0.000000 0.000000
THANK YOU!

Vous aimerez peut-être aussi