I am attempting to implement a function that sorts a randomly generated vector using selection sort. I am trying a naive way just to see if I can get it working correctly. Here is my attempt:
void selection_sort(std::vector<int>& v)
{
int pos, min, i;
//std::vector<int>::iterator pos, min, i;
for( pos = v[0]; pos < v[30]; ++pos)
{
min = pos;
for( i = v[pos + 1]; i < v[30]; ++i)
{
if( i < min)
{
min = i;
}
}
if( min != pos)
{
std::swap(v.at(min), v.at(pos));
}
}
}
For some reason however when I display the vector again, all of the elements are in the exact same order as they were originally. I am not sure if I am not using std::swap
correctly or if my selection sort is not written correctly. I am sure the answer is trivially easy, but I can not see it. Thanks for your help in advance.