954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Sorting vectors

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

newcook88
Light Poster
27 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
 

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.

monkey_king
Junior Poster
160 posts since Aug 2008
Reputation Points: 70
Solved Threads: 9
 

that link helps but how do i print the elements the the type of output vector in the code below
vector< pair > output;

newcook88
Light Poster
27 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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

s_sridhar
Junior Poster
141 posts since Mar 2009
Reputation Points: 17
Solved Threads: 16
 
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

newcook88
Light Poster
27 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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

s_sridhar
Junior Poster
141 posts since Mar 2009
Reputation Points: 17
Solved Threads: 16
 

i meant the same for that queue

s_sridhar
Junior Poster
141 posts since Mar 2009
Reputation Points: 17
Solved Threads: 16
 
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

newcook88
Light Poster
27 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

Store the frequencies in a seperate array, sort them and display the contents of the buckets.

s_sridhar
Junior Poster
141 posts since Mar 2009
Reputation Points: 17
Solved Threads: 16
 

don't make this simple thing complex, declare a 2 dimensional array of large size. and assign a[i][0]=i; if the array element exists already a[i][1]=i; Count the frequencies and store it in a seperate array 10 11 12 13 14
10 11 13 14
10 11
11 as shown. now sort the frequencies and display the contents of the corresponding buckets

s_sridhar
Junior Poster
141 posts since Mar 2009
Reputation Points: 17
Solved Threads: 16
 

how do i get it to print im an not sure how to becasue it is my first time with maps when i try the below method it is not working

for(unsigned int i=1;i
newcook88
Light Poster
27 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

i managed to print it but the sorting is done in an accessedint order i need to sort in decending order

sort(output.begin(),output.end(),sort_pred); for(unsigned int i=0;i
newcook88
Light Poster
27 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

problem solved got da rbegin and rend:)

newcook88
Light Poster
27 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You