A Sort Algorithm Problem...
Okay, so instead of keeping working on those stupid binary trees =P I figured i'd take a break and look into some sorting algorithms. I made a program which takes 3 algorithms and times how long it takes them to sort (in milliseconds).
Well there seems to be a problem with one of the algorithms... Sometimes it works, sometimes it doesnt, maybe someone can see what's wrong, here's the function:
Code:
void selectSort(vector<int> &v)
{
//Selection Sort Algorithm
int temp;
for(int i = 0; i<v.size()-1; i++)
{
temp = v.at(i);
for(int j = i+1; j<v.size()-1; j++)
{
if(v.at(j) < v.at(temp))
{
temp = j;
}
swap(v.at(i), v.at(temp));
}
}
}
When it works it sorts them right every time, but sometimes it has a run-time error. It has to do with the vector i think:
Quote:
Unhandled exception at 0x7c812a5b in sorting algorithms test 1.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0013fa08..
Thanks!