-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathMergeSort.cpp
More file actions
56 lines (49 loc) · 970 Bytes
/
MergeSort.cpp
File metadata and controls
56 lines (49 loc) · 970 Bytes
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
#include<bits/stdc++.h>
using namespace std;
void merge(int A[], int b, int m, int e)
{
int len = (e-b+1);
int B[len];
int i = b, j = m+1, k = 0;
while(i <= m && j <= e)
{
if(A[i]< A[j])
B[k++] = A[i++];
else
B[k++] = A[j++];
}
while(i <= m)
B[k++] = A[i++];
while(j <= e)
B[k++] = A[j++];
for(k = 0; k < len ; k++)
A[b++] = B[k];
}
void division(int A[], int low, int high)
{
if(low < high)
{
int mid = (low+high)/2;
division(A, low, mid);
division(A, (mid+1), high);
merge(A, low, mid, high);
}
}
int main()
{
int n,i,a[100];
cout<<"Enter the no.of elements:- \n";
cin>>n;
cout<<"Enter the elements: \n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
division(a, 0, n-1);
cout<<"The sorted array is:- \n";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
return 0;
}