forked from Sjsingh101/Basic-C-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergesort.cpp
59 lines (46 loc) · 1.35 KB
/
mergesort.cpp
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
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define F(i,a,b) for(lli i = a; i <= b; i++)
#define RF(i,a,b) for(lli i = a; i >= b; i--)
#define endl "\n"
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL);
lli cnt=0;
void merge(lli ar[ ] , lli start, lli mid, lli end) {
lli p = start,q = mid+1;
lli k = 0;
lli j ;
lli arr[end - start + 1];
while( (p <= mid) && ( q <= end) ){
if(ar[p] <= ar[q]) arr[k++] = ar[p++];
else if(ar[p] > ar[q]){
cnt += (mid-p+1);
arr[k++] = ar[q++];
}
}
while(p <= mid) arr[k++] = ar[p++];
while(q <= end) arr[k++] = ar[q++];
for(p = 0;p < k;p++) ar[start+p] = arr[p];
}
void merge_sort (lli A[ ] , lli start , lli end )
{
if( start < end ) {
lli mid = (start + end ) / 2 ; // defines the current array in 2 parts .
merge_sort (A, start , mid ) ; // sort the 1st part of array .
merge_sort (A,mid+1 , end ) ; // sort the 2nd part of array.
merge(A,start , mid , end );
}
}
int main()
{
speed;
lli n;
cin>>n;
lli a[n],b[n];
F(i,0,n-1)cin>>a[i];
F(i,0,n-1)b[i]=a[i];
merge_sort(b,0,n-1);
for(int i=0;i<n;i++)cout<<b[i]<<" ";
cout<<endl;
return 0;
}