-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merge sort.cpp
110 lines (89 loc) · 1.45 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <stdio.h>
#include "iostream"
#include <ctime>
using namespace std;
int rand_seed=10;
/* from K&R
- produces a random number between 0 and 32767.*/
int rand()
{
rand_seed = rand_seed * 1103515245 + 12345;
return (unsigned int)(rand_seed / 65536) % 32768;
}
void merge(int a[], int f, int h, int l)
{
int n1,n2;
n1 = h-f+1;
n2 = l-h;
int x[n1], y[n2];
for(int i=0; i<n1; i++)
{
x[i] = a[f+i];
}
for(int j=0; j<n2; j++)
{
y[j] = a[h+1+j];
}
int r, s, t;
r = 0;
s = 0;
t = f;
while(r<n1 && s<n2)
{
if(x[r]<=y[s])
{
a[t] = x[r];
r++;
}else
{
a[t] = y[s];
s++;
}
t++;
}
while(r<n1)
{
a[t] = x[r];
r++;
t++;
}
while(s<n2)
{
a[t] = y[s];
s++;
t++;
}
}
void merge_sort(int a[], int f, int l)
{
// h = half of array
int h;
if(l>f)
{
h = (int) (f+l)/2;
merge_sort(a, f, h);
merge_sort(a, h+1, l);
merge(a, f, h, l);
}
}
int main()
{
int size;
for(int k=1; k<=50; k++)
{
size = k*1000;
int a[size];
for(int i=0; i<size; i++)
{
a[i] = rand();
}
rand_seed=10;
unsigned t0, t1;
t0=clock();
merge_sort(a,0,size-1);
t1 = clock();
double time = (double(t1-t0)/CLOCKS_PER_SEC);
cout <<time<< endl;
}
return 0;
}