-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsorting.cpp
More file actions
80 lines (71 loc) · 1.18 KB
/
sorting.cpp
File metadata and controls
80 lines (71 loc) · 1.18 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
78
79
80
//Shivani Chander 106116084
#include <bits/stdc++.h>
using namespace std;
void merge(int *a, int l, int h, int mid)
{
int i, j, k, c[50];
i = l;
k = l;
j = mid + 1;
while (i <= mid && j <= h)
{
if (a[i] < a[j])
{
c[k] = a[i];
k++;
i++;
}
else
{
c[k] = a[j];
k++;
j++;
}
}
while (i <= mid)
{
c[k] = a[i];
k++;
i++;
}
while (j <= h)
{
c[k] = a[j];
k++;
j++;
}
for (i = l; i < k; i++)
{
a[i] = c[i];
}
}
void mergesort(int *a, int l, int h)
{
int mid;
if (l < h)
{
mid=(l+h)/2;
mergesort(a,l,mid);
mergesort(a,mid+1,h);
merge(a,l,h,mid);
}
return;
}
int main()
{
int a[100], i;
cout<<"Enter number of elements in the array"<<endl;
cin>>n;
cout<<"Enter the elements of the array"<<endl;
for (i = 0; i < n; i++)
{
cin>>a[i];
}
mergesort(a, 0, n-1);
cout<<"Sorted array is"<<endl;
for (i = 0; i < n; i++)
{
cout<<a[i]<<" ";
}
return 0;
}