Repository navigation
Expand file tree
/
Copy pathquicksort_median.cpp
More file actions
126 lines (107 loc) · 2.57 KB
/
Copy pathquicksort_median.cpp
File metadata and controls
126 lines (107 loc) · 2.57 KB
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
111
112
113
114
115
116
117
118
119
120
#include "timer.cpp"
#include "auxiliary_functions.cpp"
#include <vector>
#include <climits>
using namespace std;
void quicksortMedian(vector<int> &A, int p, int r);
int medianOfThreePartition(vector<int> &A, int p, int r);
int partition(vector<int> & A, int p, int r);
int main(int argc, char ** argv)
{
vector<int>numbers;
vector<int>holder;
if(!correctArgumentFormat(argc, argv))
{
exit(1);
}
string input_file = argv[1];
int success;
success = readInputFile(input_file, holder);
if(success != 0)
{
cout<<"error reading input file. exit."<<endl;
exit(1);
}
/*
* Begin timing sequence of:
* Quicksort (median of 3)
*/
int numRuns = 5;
struct timeval start[numRuns];
struct timeval end[numRuns];
vector<float> time;
for(int i = 0; i < numRuns; i++)
{
numbers.clear();
numbers.assign(holder.begin(), holder.end());
int array_first = 0;
int array_last = (int) numbers.size()-1;
start[i] = startTime(&start[i]);
quicksortMedian(numbers, array_first, array_last);
end[i] = endTime(&end[i]);
time.push_back(calculateTime(&start[i], &end[i]));
}
calcAvgTime(time);
/*
* End timing sequence of:
* Quicksort(median of 3)
*/
string output_file = argv[2];
success = outputToFile(output_file, numbers);
if(success != 0)
{
cout<<"error outputtting to file. exit."<<endl;
exit(1);
}
return 0;
}
void quicksortMedian(std::vector<int> &A, int p, int r){
int q;
if(p < r)
{
q = medianOfThreePartition(A, p, r);
quicksortMedian(A, p, q-1);
quicksortMedian(A, q+1, r);
}
}
//Choose three value from the vector randomly, and pick the median
int medianOfThreePartition(std::vector<int> &A, int p, int r){
/* unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator(seed);
//We use a uniform distribution in order to pseudo-guarantee that all
//values in [p,r] have an equal opportunity to appear
std::uniform_int_distribution<int> distribution(p,r);
int i = distribution(generator);
swap(A[r], A[i]);
*/
int pivot = A[(r+p)/2];
if(A[p] <= pivot && A[r] <= A[p])
{
pivot = A[p];
swap(A[r], A[p]);
}
else if(A[r] <= pivot && A[p] <= A[r])
{
pivot = A[r];
//No need to swap
}
else
{//Middle element remains the pivot
swap(A[r], A[(r+p)/2]);
}
return partition(A, p, r);
}
int partition(std::vector<int> &A, int p, int r){
int x = A[r];
int i = p-1;
for(int j = p; j < r; j++)
{
if(A[j] <= x)
{
i = i+1;
swap(A[i],A[j]);
}
}
swap(A[i+1],A[r]);
return i+1;
}