### Vector Swap?

I need help writing a sorting function that has an argument for a vector of ints rather than an array; it should use a selection sort algorithm.

Here is what I have:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596`` ``````#include #include #include using namespace std; void fillVector(vector &aVector); // PRECONDITION: number declared size of array a. // POSTCONDITION: number_used is the number of values stored in a //a[0] through a[number_used-1] have been filled with nonnegative int. void sort(vector &aVector); // PRECONDITION: takes the declared size . // the vector elements in the vector all have values. // POSTCONDITION: rearranges the vector or array so that a[0] is <= a[1].... <= a[size -1], void swapValues(int &v1, int &v2); //interchanges the values of v1 and v2 /*int smallestIndex(const int a[], int start_index, int number_used); //PRECONDITION: 0<= start_index < number_used. refferenced array elements have values. // returns te index i such that a[i] is the smallest of the values. */ int main() { vector aVector; cout << "This program sorts number from lowest to highest.\n"; fillVector(aVector); cout << " In sorted order the numbers are:\n"; sort(aVector); for (int index = 0; index < aVector.size(); index++) cout << aVector[index] << " "; cout << endl; getch(); return 0; }// end main void fillVector(vector &aVector) { int next, index = 0; cout << endl; cout << "Enter no more than 10 nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; cin >> next; while (next >= 0) { aVector.push_back(next); cin >> next; } } void sort (vector &aVector) // sorts the list of numbers using vectors { int i, j; for(i = 0; i < aVector.size() - 1; i++) { for(j = i + 1; j < aVector.size(); j++) { if(aVector[i] > aVector[j]) { swap(aVector[i], aVector[j]); } } } } void swapValues(int& v1, int& v2) { int temp; temp = v1; v1 = v2; v2 = temp; } //int smallestIndex(const int a[], int start_index, int number_used) //{ // int min = a[start_index], index_of_min = start_index; // // for (int index = start_index + 1; index < number_used; index++) // if (a[index] < min) // { // min = a[index]; // index_of_min = index; // } // // return index_of_min; //} ``````
That's a bubble sort, you shouldn't have that swap on line 70, see wikipedias page on selection sort http://en.wikipedia.org/wiki/Selection_sort
Selection sort involves finding the smallest item each iteration(and just keeping track of its index), then, at the end of the iteration swap the smallest item found with its correct position.
Why don't you just use the swap member function of vector?
http://www.cplusplus.com/reference/vector/vector/swap/
 @kempofighter Why don't you just use the swap member function of vector? http://www.cplusplus.com/reference/vector/vector/swap/

He even does not need to use the swap member function. It is enough to use simply standard function std::swap.:)
@kempofighter: because that would swap the vector.
@OP: ¿what's the problem?
I'll read up on the std::swap and give it a shot ... thank you all for taking the time to respond
I hate to say it, but I couldn't really quite figure it out @kempofighter, @vlad from moscow
In fact, you were using std::swap all along. The name of your swap function is swapValues. That function is never used.
Topic archived. No new replies allowed.