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
|
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <chrono>
using namespace std;
using namespace chrono;
void swap(string& xp, string& yp)//used to swap things
{
string temp = xp;
xp = yp;
yp = temp;
}
void bubbleSortaTOz(vector<string>& vec)
{
int i, j;
for (i = 0; i < vec.size() - 1; i++)
{
for (j = 0; j < vec.size() - i - 1; j++)
{
if (vec[j].compare(vec[j + 1]) > 0)
{
swap(vec[j], vec[j + 1]);
}
}
}
}
void bubbleSortzTOa(vector<string>& vec)
{
int i, j;
for (i = 0; i < vec.size() - 1; i++)
{
for (j = 0; j < vec.size() - i - 1; j++)
{
if (vec[j].compare(vec[j + 1]) < 0)
{
swap(vec[j], vec[j + 1]);
}
}
}
}
int main()
{
int count = 1, time;
microseconds duration;
vector<int>endVector;
vector<string>
mainVector,
aTOz,
zTOa,
randomVector;
ifstream inputFile;
ofstream outputFile;
inputFile.open("Data.csv");
outputFile.open("EndData.csv");
string line;
while (inputFile.good()) {
getline(inputFile, line, '\n');
mainVector.push_back(line);
}
inputFile.close();
for (int i = 10; i <= 3000; i *= 1.5) {
cout << "Iteration: " << count << " out of 15" << endl;
endVector.push_back(i);
for (int j = 0; j <= i; j++) {
aTOz.push_back(mainVector[j]); /*--------------------------- Apparently the error is here 'An invalid
parameter was passed to a function that considers invalid paramters fatal -- but when launched, it gives error of
- Expression: vector subscript out of range.*/
zTOa.push_back(mainVector[j]);
randomVector.push_back(mainVector[j]);
}
bubbleSortaTOz(aTOz); //sorts the data
bubbleSortzTOa(zTOa);
auto start = high_resolution_clock::now();
bubbleSortaTOz(aTOz);
auto stop = high_resolution_clock::now();
duration = duration_cast<microseconds>(stop - start);
time = duration.count();
endVector.push_back(time);
start = high_resolution_clock::now();
bubbleSortaTOz(zTOa);
stop = high_resolution_clock::now();
duration = duration_cast<microseconds>(stop - start);
time = duration.count();
endVector.push_back(time);
start = high_resolution_clock::now();
bubbleSortaTOz(randomVector);
stop = high_resolution_clock::now();
duration = duration_cast<microseconds>(stop - start);
time = duration.count();
endVector.push_back(time);
aTOz.clear();
zTOa.clear();
randomVector.clear();
count++;
}
count = 0;
for (int i = 0; i < endVector.size(); i += 4) {
if (count == 0) {
outputFile << "Data" << "," << "'Fastest Time'" << "," << "'Slowest time'" << "," << "'Average time'" << endl;
count = 1;
}
outputFile << endVector[i] << "," << endVector[i + 1] << "," << endVector[i + 2] << "," << endVector[i + 3] << endl;
}
outputFile.close();
return 0;
}
|