-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComparisonCount_MergeSort.cpp
More file actions
61 lines (47 loc) · 1.04 KB
/
Copy pathComparisonCount_MergeSort.cpp
File metadata and controls
61 lines (47 loc) · 1.04 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
#include<iostream>
using namespace std;
int const maxN=28;
int merge(int a[], int l, int m, int r)
{ int i, j, count=0;
static int aux[maxN];
for (i = m+1; i > l; i--) aux[i-1] = a[i-1];
for (j = m; j < r; j++) aux[r+m-j] = a[j+1];
for (int k = l; k <= r; k++)
if (aux[j] < aux[i]) // array elements are compared
{
a[k] = aux[j--];
count++;
}
else
{
a[k] = aux[i++];
count++;
}
return count;
}
int mergesort(int a[], int l, int r)
{
int count=0;
if (r <= l) return 0;
int m = (r+l)/2;
count += mergesort(a, l, m);
count += mergesort(a, m+1, r);
count += merge(a, l, m, r);
return count;
}
int main()
{
int input[maxN];
for(int i=0;i<maxN;i++)
{
cin>>input[i];
}
int compCount = mergesort(input,0,maxN-1);
cout<<"After Merge Sort, The sorted Array is "<<endl;
for(int i=0;i<maxN;i++)
{
cout<<input[i]<<" ";
}
cout<<endl;
cout<<"The number of comparisons performed in merge sort is "<<compCount<<endl;
}