-
Notifications
You must be signed in to change notification settings - Fork 0
/
Insertion sort.cpp
66 lines (49 loc) · 1 KB
/
Insertion 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
#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 insertion_sort(int a[], int n)
{
int key, s, r;
for(r=1; r<n; r++)
{
key = a[r];
s = r-1;
while(s>=0 && a[s]>key)
{
a[s+1] = a[s];
s--;
}
a[s+1] = key;
}
}
int main()
{
int size;
unsigned t0, t1;
for(int k=1; k<=50; k++)
{
size = 1000*k;
int a[size];
//Fill array
for(int i=0; i<size; i++)
{
a[i] = rand();
}
rand_seed=10;
t0=clock();
insertion_sort(a,size);
t1 = clock();
double time = (double(t1-t0)/CLOCKS_PER_SEC);
cout <<time<< endl;
}
return 0;
}