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

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.

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;

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

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

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

i meant the same for that queue

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

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

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

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;
}

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;
}

problem solved got da rbegin and rend:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.