I'm trying to sort a vector using insertion sort but for most values it fails to finish. if the vector size is greater than 3 the loop will not finish for an extended period of time, not at all, or it will finish quickly as expected up to values of 5 randomly.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <vector>
void PRINT(std::vector<int> &data);
void insertion(std::vector<int> data, clock_t Time);
int main()
{
clock_t Time;
int dataSize, maxval;
std::vector<int> data;
srand(time(0));
std::cout<<"input vector size\n";
std::cin>>dataSize;
std::cout<<"input max value\n";
std::cin>>maxval;
Time=clock();
for(int i=0; i<dataSize; i++)
{
data.push_back(1+rand()%maxval);
}
Time=clock()-Time;
std::cout<<"ticks to randomize "<<Time<<" seconds "<<float(Time)/CLOCKS_PER_SEC<<std::endl;
insertion(data, Time);
return 0;
}
void PRINT(std::vector<int> &data)
{
std::cout<<"data contains: ";
for(int i=0; i<data.size(); i++)
std::cout<<data[i]<<", ";
std::cout<<std::endl;
}
void insertion(std::vector<int> data, clock_t Time)
{
bool sorted;
std::vector<int>::iterator j;
Time=clock();
for(int i=1; i<data.size(); i++)
{
j=(data.begin()+i-1);
sorted=false;
while(!(j==data.begin())&&!sorted)
{
if (*j<data[i])
{
sorted=true;
data.insert((j+1),data.at(i));
}
j--;
}
}
Time=clock()-Time;
std::cout<<"insertion:\nticks taken "<<Time<<"\n";
std::cout<<"seconds "<<float(Time)/CLOCKS_PER_SEC<<std::endl;
PRINT(data);
}
does anyone see why my implementation has such an insane run time at higher values? is there a better way to implement this?