I was wondering how to sort a vector in numerical order... Is it similar to a "bubble sort" for an array... I can post the array sort code if need be...
I was wondering how to sort a vector in numerical order... Is it similar to a "bubble sort" for an array... I can post the array sort code if need be...
yes you can do a bubble sort on a vector. You can also do a number of other sorts on a vector. You can also call the sort() function in STL to sort a vector.
As far as I know, vectors implement the sort algorithm.
http://www.sgi.com/tech/stl/sort.html
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I need to use the bubble sort because I need to sort one vector numerically (101, 102, etc...) and then resort another vector into the proper order...
for example:
the values in the first vector are
102
103
101
105
104
and the values in the second vector are
202.22
122.32
122.33
102.00
902.11
when side by side:
102 202.22
103 122.32
etc...
They need to be arranged:
101 122.33
102 202.22
102 122.32
etc...
You could try to dump the values from the two vectors into a map<int,double> container. They would then be automatically sorted as you indicated. Very easy to do.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
This is the generic bubble sort algorithm that I use... how would it be modified to use a vector?
Code:void SortArray (int NumberOfItems, int List []) { bool ExchangeMade; int Pass = 0, Index; int Number; ExchangeMade = true; while ((Pass < NumberOfItems - 1) && ExchangeMade == true) { ExchangeMade = false; Pass ++; for (Index = 0; Index < (NumberOfItems - Pass); Index++) if (List [Index] > List [Index + 1]) { Number = List [Index]; List [Index] = List [Index +1]; List [Index + 1] = Number; ExchangeMade = true; } } }
If you have a bubble sort that works with an array, than it will work with a vector. Since you have two vectors, I would assume that for every move you make with the values in the first vector you could make the same move with the values in the second vector.
[Edit] - Ahh, you posted your bubble sort ... You can just pass in your two vectors and do the same thing. You don't need to pass the size anymore since that is saved inside the vector.
that's all I wanted to know... I already know how to switch the values for more than one array at a time... I just wanted to make sure I could use the same algorithm or not...
Thanks!
BTW... how would I implement that map<int,double> thing? I probably won't do it for this project, but maybe for future reference?
For the map, instead of adding the data to the vector, you just add it to a map. Then it is already sorted for you (if you use the STL map).I just used rand() to come up with numbers, obviously you are getting your values from somewhere else. Since the map holds a pair of objects - key and value - you can use make_pair to insert the pair into the map. In your case, 101 is a key and 122.33 is a value. The STL map automatically sorts by key, and since your key is an int its especially easy.Code:#include <iostream> #include <map> #include <cstdlib> #include <ctime> int main() { srand(time(0)); std::map<int, int> m; for (int i = 0; i < 20; i++) { m.insert(std::make_pair(rand() % 100, rand() % 1000)); } std::map<int, int>::const_iterator iterPair = m.begin(); std::map<int, int>::const_iterator iterEnd = m.end(); for (; iterPair != iterEnd; ++iterPair) { std::cout << "FirstNum: " << iterPair->first; std::cout << "\tSecondNum: " << iterPair->second << std::endl; } }
oops...
Last edited by criticalerror; 12-11-2003 at 07:39 PM.