How to Bubble Sort a Map in C++?

i know how to sort a array, but i cannot made bubble sort in map :/ if possible to tell me a bit about it..

its:
1
2
map<string,int> op;
map<string,int>::iterator opi;


do i have to do for loop with iterator?

its hard because i dont have index right here .. in arrays i do ://

the point is from low to high

..
i was trying to do something like this .. but i failed
1
2
3
4
5
6
for(opi = op.begin(); opi != op.end(); opi++){
		int Current = opi->second; 
		opi++;
		int Next = opi->second;
		opi--;
// and continue the code 

but sadly .. it wont let me .. because opi++ is error line..
since there is way that when it reaches end .. it asks for increment which is not possible so it cant be like that .. there should be another way .. tnx in advance...
Last edited on
Maps are already sorted by key.
If you want to sort values... then you lose key→value mapping making choice of map wrong.
I think you have data type choice problem here.

Of course if it is realy what do you want, there is a way... Just make sure it is what do you want.
well i was thinking about that .. but isnt possible to move both in same time?
1
2
3
4
5
6
7
8
9
1. get current key and value
2. increment iterator
3. get next key and value
4. decrement iterator
5. if current value > next value
   1-save current key and value somewhere
   2-move next key and value to current position
   3-move current key and value to next position
6. do recursion if needed

i was planning on this
btw im trying map sorting for the first time...

yes its correct, they are sorted by key by default, but i wanted to sort them by value
Last edited on
std::map maintains a correspondence between key and value.
If the correspondence is simply to sort both and relate the ones that are in the same position, you could just maintain two arrays.

you could sort as you want by using an aux array, transfer all the values of the map, sort the array, transfer the array to the map.


> since there is way that when it reaches end .. it asks for increment which is not possible
if it reached the end, it would terminate the loop
the problem is that it may reach the end at line 3, and then you try to dereference it.

bubble sort is an awful algorithm, if you insist on doing the sort yourself consider selection sort.
1
2
3
for( it: m )
   min = min_element( transform(m, .second) )
   iter_swap( it, min )
Why are you using the wrong keys for the map? If you want to have the integer values sorted, then why not declare map with int as key and string as value?

If you want both int's and strings sorted, then place both in 2 different sets and finally insert into the map in the order they appear in the sets.

You are making this more complicated than it should be
no i must have string,int map
and i was thinking to make it both sorted .. but when i think now .. i can see its impossible because if its

1
2
3
4
key - value
"sr001truck" - 9
"sr002truck" - 3
"sr003truck" - 16


if you sort this .. 9 should be after 3 .. and when you swap them.. it would look like this:

1
2
3
4
key - value
"sr002truck" - 3
"sr001truck" - 9
"sr003truck" - 16


which is impossible i think because map is default sort by key .. im a wrong here?

really sorry im still learning about maps.... :////
In your case you need to either swap key and value (making it map<int, string>, maybe using multimap if integers are not unique) or fall back to the vector<pair<string, int>> and using sort() with custom comparator. What are you doing now is like trying to insert USB cable sideways and insisting that you must need to insert it exactly that way.
What are you doing now is like trying to insert USB cable sideways and insisting that you must need to insert it exactly that way.


I'm stealing this lol
hehe i know im being funny :D

ok, i will try that, swap and put them in second map int,string ..

done i done it ))
Last edited on
Topic archived. No new replies allowed.