Académique Documents
Professionnel Documents
Culture Documents
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
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
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
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
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
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)
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.)
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
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 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);
• 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);
• }
• }
• 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