-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.C
More file actions
139 lines (124 loc) · 2.86 KB
/
quickSort.C
File metadata and controls
139 lines (124 loc) · 2.86 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Hung Nguyen CSCE350 Project 1
#include <iostream>
#include <fstream>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <iomanip>
#include <unistd.h>
#include <sys/time.h>
#include <sstream>
#include <string.h>
#include <string>
using namespace std;
const int MIN = 10;
const int MAX = 100000;
const int FILE_SIZE = 100;
const char* executionTime = "Nguyen_Hung_timeOfExecution.txt";
const char* averageExecutionTime = "Nguyen_Hung_averageExecutionTime.txt";
void quickSort(int start, int end, vector<double>& numbs){
if(start >= end)
return;
int x = start;
double y;
int i = start + 1, j = end - 1;
while(i <=j){
while(i < end && numbs[i] <= numbs[x])
++i;
while(j > start && numbs[j] >= numbs[x])
--j;
if(i < j){
y = numbs[i];
numbs[i] = numbs[j];
numbs[j] = y;
}
}
if(i == end){
y = numbs[x];
numbs[x] = numbs[end - 1];
quickSort(start, end-1, numbs);
}
else if ( j == start){
quickSort(start + 1, end, numbs);
}
else {
if(i == j)
j--;
y = numbs[x];
numbs[x] = numbs[j];
numbs[j] = y;
quickSort(start, j, numbs);
quickSort(j + 1, end, numbs);
}
}
void outputExecutionTime(int size, double time){
ofstream out(executionTime, std::ios::app);
out << setw(10) << size << " " << fixed << setw(15) << showpoint << time << endl;
out.close();
}
void outputAverageExecutionTime(){
ifstream in;
ofstream out;
in.open(executionTime);
out.open(averageExecutionTime);
out << setw(10) << "Input Size " << setw(22) << "Average Execution Time" << endl;
string item;
int size;
double sum = 0;
double time;
in >> item;
in >> item;
in >> item;
in >> item;
for(int i = MIN; i <= MAX; i *= 10){
sum = 0;
for(int j = 0; j < FILE_SIZE; j++){
in >> size >> time;
sum += time;
}
out << setw(10) << size << " " << setw(22) << sum / 100.0 << endl;
sum = 0;
}
out.close();
}
void sortIt(const char* inputFile, const char* outputFile){
ifstream in;
ofstream out;
in.open(inputFile, std::ios::in);
if(!in.is_open()){
cerr << "Input file cannot be found!" << endl;
exit(-1);
}
vector<double> numbs;
double theNumb;
while(in >> theNumb){
numbs.push_back(theNumb);
}
in.close();
double time;
struct timeval beg, end;
gettimeofday(&beg, NULL);
quickSort(0, numbs.size(), numbs);
gettimeofday(&end, NULL);
time = (double)((end.tv_sec * 1000000 + end.tv_usec) - (beg.tv_sec * 1000000 + beg.tv_usec)) / 1000.0;
out.open(outputFile);
if(!out.is_open()){
cerr << "output file cannot be found!" << endl;
exit(-1);
}
for(int i = 0; i < numbs.size(); i++){
out << numbs[i] << " ";
}
out.close();
outputExecutionTime(numbs.size(), time);
}
int main(int argc, char* argv[]){
if(argc < 3) {
cerr << "Not enough arguments entered!" << endl;
return -1;
}
sortIt(argv[1], argv[2]);
outputAverageExecutionTime(); // Calculating AvgExecutionTime
//from executionTime file
return 0;
}