forked from ambujraj/hacktoberfest2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_sort.cpp
83 lines (78 loc) · 1.17 KB
/
merge_sort.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int no_comp=0;
void merge(int ar[],int larr[],int lsize,int rarr[],int rsize)
{
int i=0,j=0,k=0;
while(i<lsize && j<rsize)
{
if(larr[i]<rarr[j])
{
no_comp++;
ar[k]=larr[i];
k++;
i++;
}
else
{
no_comp++;
ar[k]=rarr[j];
k++;
j++;
}
}
while(i<lsize)
{
ar[k]=larr[i];
i++;
k++;
}
while(j<rsize)
{
ar[k]=rarr[j];
k++;
j++;
}
}
void merge_sort(int ar[],int size)
{
int mid=size/2;
if(mid<1)
return;
int lsize=mid;
int rsize=size-mid;
int larr[lsize];
int rarr[rsize];
for(int i=0;i<mid;i++)
{
larr[i]=ar[i];
}
for(int i=0, j=mid;j<size;j++,i++)
{
rarr[i]=ar[j];
}
merge_sort(larr,lsize);
merge_sort(rarr,rsize);
merge(ar,larr,lsize,rarr,rsize);
}
int main()
{
int size,temp,min;
cout<<"\n Size of array \t No. of comparisions";
// 100 times different array to sort
for(int m=0;m<100;m++)
{
no_comp=0;
// size of array is also random between 1000
size=rand()%971+30;
cout<<"\n\t"<<size;
int ar[size];
for(int i=0;i<size;i++)
ar[i]=rand()%10;
merge_sort(ar,size);
cout<<"\t\t"<<no_comp;
}
getch();
}