-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSort.cpp
executable file
·60 lines (52 loc) · 1.15 KB
/
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
/**
* Time a function to compute Fibonacci numbers.
* Created: 12:04, 09/06/03
* Modified: 21:39, 09/19/03
*
*/
#include <iostream>
#include <iomanip>
#include <sys/timeb.h>
// #include <time.h>
using namespace std;
// returns the time in milliseconds.
long millis()
{
struct timeb t;
ftime(&t);
return 1000*t.time + t.millitm;
}
void fillArray(int a[], int n)
{
for( int i = 0; i < n; i++ )
a[0] = rand();
}
void sort(int a[], int n)
{
for( int i = 0; i < n; i++ ) {
for( int j = 0; j < n-1; j++ ) {
if( a[j] > a[j+1] ) {
int t = a[j];
a[j] = a[j+1];
a[j+1] = a[j];
}
}
}
}
int main()
{
const long start = 0;
const long limit = 200000;
const long step = 5000;
for( long n = start; n < limit; n += step ) {
int *a = new int[n];
fillArray(a, n);
long t0 = millis();
sort(a,n);
long t1 = millis();
cout << setw(15) << (t1-t0)
<< setw(15) << n
<< endl;
delete a;
}
}