-
Notifications
You must be signed in to change notification settings - Fork 477
Expand file tree
/
Copy pathMergeSort.java
More file actions
77 lines (69 loc) · 1.36 KB
/
MergeSort.java
File metadata and controls
77 lines (69 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* Merge Sort Iterative Implementation In C++
complexity : O(n)
*/
#include<bits/stdc++.h>
using namespace std;
#define M 10
void input(int *a)
{
cout<<"\nArray : ";
for(int i=0;i<M;i++)
cin >> a[i];
}
void merge(int *a)
{
int tmp[M],i,j,k,size=1;
int l1,u1,l2,u2;
while(size<M)
{
k=0;
l1=0;
while(l1+size<M)
{
l2=l1+size;
u1=l2-1;
if(l2+size<M)
u2=l2+size-1;
else
u2=M-1;
//Comparisions
for(i=l1,j=l2;i<=u1&&j<=u2;k++)
{
if(a[i]<a[j])
tmp[k]=a[i++];
else
tmp[k]=a[j++];
}
//Remaining Elements
for(;i<=u1;i++)
tmp[k++]=a[i];
for(;j<=u2;j++)
tmp[k++]=a[j];
l1=u2+1;
}
for(int i=l1;i<M;i++)
tmp[k++]=a[i];
for(int i=0;i<M;i++)
a[i]=tmp[i];
//doubling Size
size*=2;
}
}
void display(int *a)
{
cout<<"\nArray : ";
for(int i=0;i<M;i++)
cout<<a[i]<<' ';
}
int main()
{
//Sample size M = 10
//Array of size 10
int x[M];
input(x);
cout<<"\nBefore sorting : ";
display(x);
cout<<"\nAfter sorting : ";
merge(x);
display(x);
}