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...

Printable View

- 12-11-2003criticalerrorSorting a vector numerically
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...

- 12-11-2003elad
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.

- 12-11-2003Salem
As far as I know, vectors implement the sort algorithm.

http://www.sgi.com/tech/stl/sort.html - 12-11-2003criticalerror
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... - 12-11-2003hk_mp5kpdw
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. - 12-11-2003criticalerror
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;

}

}

}

- 12-11-2003jlou
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. - 12-11-2003criticalerror
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? - 12-11-2003jlou
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).

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;

}

}

- 12-11-2003criticalerror
oops...