I am trying to calculate the running time of an algorithm quicksort which is in
ID: 3877044 • Letter: I
Question
I am trying to calculate the running time of an algorithm quicksort which is in a seperate file. I have all the elements in an array, but I need to pass it to the function in quicksort to sort it. it needs to also return the time that it took to do it. I need to use Ctime. can someone help me impliment the main code?
for (std::vector<std::string>::iterator itr = keys.begin(); itr != keys.end(); ++itr) {
std::vector<int>data = jsonObject[*itr].get<std::vector<int>>();
}
} //this works
//pass vector to quicksort:
//void QuickSort(std::vector<int>* numbers) the prototype in other file is this:
Explanation / Answer
See this test program, you will get an idea how to compute time and pass vector into quick sort
/************************/
#include <iostream>
#include <ctime>
#include <string>
#include <vector>
using namespace std;
void QuickSort(std::vector<int>* numbers){
//put your quick sort algorithm here
for(int i=0;i<10000000;i++); //this line is inserted for testing purpose
}
int main()
{
double start, end, diff; //define time variables
vector<string> keys; //define your vector here
//"Hello", "world" is pushed for testing purpose
keys.push_back("Hello");
keys.push_back("world");
vector<vector<string> > jsonObject; //define jsonObject with exact datatype
for (std::vector<std::string>::iterator itr = keys.begin(); itr != keys.end(); ++itr) {
//give proper assignment for below line
std::vector<int> data;// = jsonObject[*itr].get<std::vector<int>>();
start = clock(); //call clock() to get start of quick sort
QuickSort(&data); //call quick sort
end= clock(); //call clock() to get end of quick sort
diff = ((end-start)/double(CLOCKS_PER_SEC))*1000; //compute time in milliseconds
cout <<"Time taken: "<<diff<<" milli seconds"<<endl;
}
return 0;
}
/*********************/output
Time taken: 32.687 milli seconds
Time taken: 32.158 milli seconds
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.