Hello everyone. I am working on creating a stub that would allow me to swap out various sorting algorithms so that I can "plug in" the algorithms and run the various sorts. The below code is for Insertion Sort (yes, I know it is the slowest possible sort), but once I get it to work, I plan on doing Heap Sort, Merge Sort, Quick Sort, Radix Sort, and TimSort while collecting times from each one. The input file I am using has approximately 1/2 million values in order to give a better example of the variences in time.
I have included code for a timer to start just before the sorting algorithm and end just after the algorithm, then outputting the total amount of time the sort took. This information will be used to show quantifiable data for explaining the differences between the various choices of algorithms.
I've never seen the "Vector Subscript Out of Range Error" and can't determine the issue from researching C++ Forums or Daniweb. Can you point me in the right direction?
Thank you,
#include<iostream>
#include<iterator>
#include<fstream>
#include<vector>
#include<ctime>
using namespace std;
int main()
{
int i=0, j=0, key=0;
std::vector<int> a;
int start_s=clock(); //start timer
ifstream readFile;
readFile.open("inputData.txt");//open file
if (readFile.is_open())
{
while (!readFile.eof())
while (readFile >> a[i])
{
for (int i=1; i<a.size(); ++i) // use pre-increment to avoid unneccessary temporary object
{
key= a[i];
j = i-1;
while((j >= 0) && (a[j] > key))
{
a[j+1] = a[j];
j -= 1;
}
a[j+1]=key;
}
}
}
readFile.close(); //close file
int stop_s=clock();//stop timer
cout <<"time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000 << endl;//output timer
return 0;
}