Sorting vectors

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2009
Posts: 27
Reputation: newcook88 is an unknown quantity at this point 
Solved Threads: 0
newcook88 newcook88 is offline Offline
Light Poster

Sorting vectors

 
0
  #1
May 12th, 2009
How do i sort a vector based on the frequency of its elements

example

my vector contains 10,11,12,10,11,13,11

i need to get the sorted out put as
11,11,11,10,10,12,13

the normal sort is

sort(vec.begin(), vec.end());
how do i sort it in that way
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 630
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Sorting vectors

 
1
  #2
May 12th, 2009
Here is what you are trying to do:
http://answers.yahoo.com/question/in...2195554AAANJgl
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

Re: Sorting vectors

 
0
  #3
May 12th, 2009
What is the ordering?
If you want to sort by the most frequent elements first, then you can use the link given to you.

If you are sorting by some other rule,
you should say what rule that is.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 27
Reputation: newcook88 is an unknown quantity at this point 
Solved Threads: 0
newcook88 newcook88 is offline Offline
Light Poster

Re: Sorting vectors

 
0
  #4
May 13th, 2009
that link helps but how do i print the elements the the type of output vector in the code below
vector< pair<int, string> > output;
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 110
Reputation: s_sridhar has a little shameless behaviour in the past 
Solved Threads: 11
s_sridhar's Avatar
s_sridhar s_sridhar is offline Offline
Junior Poster

Re: Sorting vectors

 
0
  #5
May 13th, 2009
Won't this be easy, just maintain seperate queue for each element and insert the element into the corresponding queue by dynamic memory allocation, then the queue for which front-reverse is the maximum will have maximum frequency
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 27
Reputation: newcook88 is an unknown quantity at this point 
Solved Threads: 0
newcook88 newcook88 is offline Offline
Light Poster

Re: Sorting vectors

 
0
  #6
May 13th, 2009
Originally Posted by s_sridhar View Post
Won't this be easy, just maintain seperate queue for each element and insert the element into the corresponding queue by dynamic memory allocation, then the queue for which front-reverse is the maximum will have maximum frequency
can you please show how its done
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 110
Reputation: s_sridhar has a little shameless behaviour in the past 
Solved Threads: 11
s_sridhar's Avatar
s_sridhar s_sridhar is offline Offline
Junior Poster

Re: Sorting vectors

 
0
  #7
May 13th, 2009
or rather, may be you can use bucket sort, declare a two dimensional array provided you already know the range. just push the number into the corresponding bucket and based on the frequency you can then push it into the stack
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 110
Reputation: s_sridhar has a little shameless behaviour in the past 
Solved Threads: 11
s_sridhar's Avatar
s_sridhar s_sridhar is offline Offline
Junior Poster

Re: Sorting vectors

 
0
  #8
May 13th, 2009
i meant the same for that queue
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 27
Reputation: newcook88 is an unknown quantity at this point 
Solved Threads: 0
newcook88 newcook88 is offline Offline
Light Poster

Re: Sorting vectors

 
0
  #9
May 13th, 2009
Originally Posted by s_sridhar View Post
i meant the same for that queue
sir can you show me how to print the sorted values to screen in da example providen in the yahoo llink.. i am having trouble displaying the output
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 110
Reputation: s_sridhar has a little shameless behaviour in the past 
Solved Threads: 11
s_sridhar's Avatar
s_sridhar s_sridhar is offline Offline
Junior Poster

Re: Sorting vectors

 
0
  #10
May 13th, 2009
Store the frequencies in a seperate array, sort them and display the contents of the buckets.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC