DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Sorting vectors (http://www.daniweb.com/forums/thread191784.html)

newcook88 May 12th, 2009 1:15 pm
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

daviddoria May 12th, 2009 7:45 pm
Re: Sorting vectors
 
Here is what you are trying to do:
http://answers.yahoo.com/question/in...2195554AAANJgl

monkey_king May 12th, 2009 9:44 pm
Re: Sorting vectors
 
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 May 13th, 2009 12:12 am
Re: Sorting vectors
 
that link helps but how do i print the elements the the type of output vector in the code below
Quote:

vector< pair<int, string> > output;

s_sridhar May 13th, 2009 1:53 am
Re: Sorting vectors
 
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 May 13th, 2009 2:22 am
Re: Sorting vectors
 
Quote:

Originally Posted by s_sridhar (Post 867688)
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 May 13th, 2009 3:00 am
Re: Sorting vectors
 
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 May 13th, 2009 3:01 am
Re: Sorting vectors
 
i meant the same for that queue

newcook88 May 13th, 2009 3:18 am
Re: Sorting vectors
 
Quote:

Originally Posted by s_sridhar (Post 867720)
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 May 13th, 2009 3:20 am
Re: Sorting vectors
 
Store the frequencies in a seperate array, sort them and display the contents of the buckets.


All times are GMT -4. The time now is 5:50 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC