| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- #include <cstdio>
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <ctime>
- #include <vector>
- #include "json.hpp"
- #include "insertionsort.h"
- #include "mergesort.h"
- #include "quicksort.h"
- int main(int argc, char* argv[]) {
- // read JSON file and store in jsonObject
- std::ifstream file;
- file.open(argv[1]);
- nlohmann::json jsonObject;
- // Store the contents filename into jsonObject
- if (file.is_open()) {
- file >> jsonObject;
- }
- // initiate CSV file quickto which data will be written
- std::ofstream theFile;
- theFile.open("timealgorithms.csv");
- theFile << "Sample,InsertionSortTime,InsertionSortCompares,InsertionSortMemaccess,MergeSortTime,MergeSortCompares,MergeSortMemaccess,QuickSortTime,QuickSortCompares,QuickSortMemaccess\n";
- // initialize arrays for JSON data
- // each should have it's own copy cause the array get's sorted within each function
- std::vector<int>* insArray = new std::vector<int>();
- std::vector<int>* merArray = new std::vector<int>();
- std::vector<int>* quickArray = new std::vector<int>();
- // initialize insertion sort variables
- int *insReturnArray;
- time_t insStartTime;
- time_t insTime;
- // initialize merge sort variables
- int *mergeReturnArray;
- time_t merStartTime;
- time_t merTime;
- // initialize quick sort variables
- int *quickReturnArray;
- time_t quickStartTime;
- time_t quickTime;
- std::string sampleName; // Sample key name to iterate
- for (auto itr = jsonObject.begin(); itr != jsonObject.end(); ++itr) {
- sampleName = itr.key();
- if (sampleName != "metadata") {
- for (auto arrayItr = jsonObject[sampleName].begin(); arrayItr != jsonObject[sampleName].end(); ++arrayItr) {
- insArray -> push_back(*arrayItr);
- merArray -> push_back(*arrayItr);
- quickArray -> push_back(*arrayItr);
- }
- // Add the sample name to the CSV file line
- theFile << itr.key() << ",";
- // insertion sort evaluation
- insStartTime = time(0);
- insReturnArray = InsertionSort(insArray);
- insTime = time(0) - insStartTime;
- // Add the insertion sort data to the CSV file line
- theFile << insTime << "," << *insReturnArray << "," << *(insReturnArray + 1) << "," ;
- // merge sort evaluation
- merStartTime = time(0);
- mergeReturnArray = MergeSort(merArray);
- merTime = time(0) - merStartTime;
- // Add the merge sort data to the CSV file line
- theFile << merTime << "," << *mergeReturnArray << "," << *(mergeReturnArray + 1) << "," ;
- // quick sort evaluation
- quickStartTime = time(0);
- quickReturnArray = QuickSort(quickArray);
- quickTime = time(0) - quickStartTime;
- // Add the quick sort data to the CSV file line
- theFile << quickTime << "," << *quickReturnArray << "," << *(quickReturnArray + 1);
- theFile << "\n";
- // clear the arrays for the next sample
- insArray -> clear();
- merArray -> clear();
- quickArray -> clear();
- }
- }
- theFile.close(); // close CSV file
- file.close(); // close JSON file
- return 0;
- }
|