-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoddEvenSort.cpp
66 lines (59 loc) · 1.51 KB
/
oddEvenSort.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 <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
void swap(unsigned int &x, unsigned int &y) {
int z = x;
x = y;
y = z;
}
void oddEvenSort(unsigned int* toSort, unsigned int length)
{
bool sorted;
while(!sorted){
sorted = true;
for(int i = 1; i<length-1; i+=2){
if(toSort[i] > toSort[i+1]){
sorted = false;
swap(toSort[i], toSort[i+1]);
}
}
for(int i=0; i<length-1; i+=2){
if(toSort[i] > toSort[i+1]){
sorted = false;
swap(toSort[i], toSort[i+1]);
}
}
}
}
void InitArrayWithRandomValues(unsigned int A[], unsigned long int n) {
for (unsigned long int k = 0; k < n; k++)
A[k] = rand();
}
unsigned int GetCurrentTime() { // in hundredths of second
struct timeval time;
gettimeofday(&time, NULL);
return time.tv_sec%10000*100 + time.tv_usec/10000;
}
void TestAlgorithm() {
unsigned int * A = (unsigned int *) malloc(30000L * sizeof(int));
for (int cnt = 1; cnt <= 100; cnt++) { // cnt = number of experiments
for (unsigned int k = 1; k <= 10; k++) {
if (k > 1) printf("\t");
unsigned long int n = 3000L * k; // n = actual array length
InitArrayWithRandomValues(A,n);
unsigned long int before = GetCurrentTime();
oddEvenSort(A,n);
unsigned long int after = GetCurrentTime();
unsigned long int TimeElapsed = after - before; // in centiseconds
// Not to be run about midnight!
printf("%u", TimeElapsed);
}
printf("\n");
}
free(A);
}
int main() {
TestAlgorithm();
return 0;
}