#include #include #include #include #include #include #include #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* insArray = new std::vector(); std::vector* merArray = new std::vector(); std::vector* quickArray = new std::vector(); // initialize insertion sort variables int *insReturnArray; double insStartTime; double insTime; // initialize merge sort variables int *mergeReturnArray; double merStartTime; double merTime; // initialize quick sort variables int *quickReturnArray; double quickStartTime; double 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; std::cout << "insertion sort comparison counter: " << *insReturnArray << std::endl; std::cout << "insertion sort memory access counter: " << *(insReturnArray + 1) << std::endl; std::cout << "wallclock time for insertion sort: " << insTime << std::endl; // 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; std::cout << "merge sort comparison counter: " << *mergeReturnArray << std::endl; std::cout << "merge sort memory access counter: " << *(mergeReturnArray + 1) << std::endl; std::cout << "wallclock time for merge sort: " << merTime << std::endl; // 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; std::cout << "quick sort comparison counter: " << *quickReturnArray << std::endl; std::cout << "quick sort memory access counter: " << *(quickReturnArray + 1) << std::endl; std::cout << "wallclock time for quick sort: " << quickTime << std::endl; std::cout << std::endl; // Add the quick sort data to the CSV file line theFile << quickTime << "," << *quickReturnArray << "," << *(quickReturnArray + 1); theFile << "\n"; // clear the array for the next sample insArray -> clear(); merArray -> clear(); quickArray -> clear(); } } theFile.close(); // close CSV file file.close(); // close JSON file return 0; }