I am working on a program that has me insert random integers into two vectors, and sort them using selection sort.
I was wondering if I was on the right path of trying to swap the elements in the vector.
I use the swap() function, correct?
also, to insert random integers into the vector, I am supposed to use the random function. This is what I have
I am not sure if I would need a for loop for it, as I am not in charge of writing main(). Most of the program is already wrote for us, and she gave us the requirements for what we are supposed to write. She does not say insert "This many numbers", so I assume that she does that in her main program.
I was wondering if I was on the right path of trying to swap the elements in the vector.
Code:
void selectionSort(vector <int>& v)
{//begin selectionSort
int i, j, min, minat;
for (i = 0; i < (v.size() - 1); i++)
{//begin for
minat = i;
min = v.at(i);
for (j = (i+1); j < v.size(); j++)
{//begin inner for
if (min > v.at(j))
{//begin if
minat = j;
min = v.at(j);
}//end if
}//end inner for
swap (v.at(i), v.at(minat); //Vector swap
}//end for
}//end selectionSort
I use the swap() function, correct?
also, to insert random integers into the vector, I am supposed to use the random function. This is what I have
Code:
v1.push_back(srand(s1));
I am not sure if I would need a for loop for it, as I am not in charge of writing main(). Most of the program is already wrote for us, and she gave us the requirements for what we are supposed to write. She does not say insert "This many numbers", so I assume that she does that in her main program.