## newcook88

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

## monkey_king 39

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.

## newcook88

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;

## s_sridhar 7

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

## newcook88

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

## s_sridhar 7

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 7

i meant the same for that queue

## newcook88

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

## s_sridhar 7

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

## s_sridhar 7

don't make this simple thing complex, declare a 2 dimensional array of large size. and assign a[0]=i; if the array element exists already a[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

## newcook88

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<output.size ();i++)
{
cout<<output[i]<<endl;
}
``````

## newcook88

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<output.size() ;i++)
{
cout<<output[i].second <<endl;
}
``````

## newcook88

problem solved got da rbegin and rend:)