Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,173 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,952 people online right now. Registration is fast and FREE... Join Now!




Help with sorting

 
Reply to this topicStart new topic

Help with sorting

alexander1975
14 Dec, 2007 - 06:43 PM
Post #1

New D.I.C Head
*

Joined: 14 Dec, 2007
Posts: 1


My Contributions
Hi there, I'm new at this community but I hope that you will want to help me fix this...
Let's get to the point,

I am making a pretty complex program but, as this is just a small part of it, I'll try to make this as simple as I can.

The fact is that I have got 2 vectors ( 0..M-1 ):

-You can think of the first one as if the first one was a ranking, where each position stores the identifier of the person that is in that position.
-The second vector has in each position ( where the index itself represents the (identification of the person-1) ) a score.

So as an example.
rank = [3,1,2] and pple = [10,20,30]-> so the first of the ranking (rank[0]) is the 3, with 10 points.



I have to make a sorting algorithm for this, so that the rank is sorted as the scores decrease.
Moreover, when two or more of the people have got the same score they have to be sorted by identifier ( increasing ):

Something like this:

if people = [10,20,10,30,25] then rank = [4,5,2,1,3]

I've been using quicksort and I've had some trouble with it.

The fact is that now i can sort most of the cases... but if i got different people with the same score(more than one).


CODE

void QS( ranking, pple, int esq, int dret){
   int i,j,pivot,aux;
   int X, Y;
   i = esq;
   j = dret;
   pivot = pple[ranking[ ((i+j) / 2 ) ])-1];;
   do{
      while( ( X = pple[ranking[i]-1] ) >  pivot ){
         i++;
      }
      while( ( Y = pple[ranking[j]-1] ) <  pivot ){
         j--;
      }
      if( i <= j ){
         if( X != Y ){
            aux = ranking[ i ];
            ranking[ i ] = ranking[ j ];
            ranking[ j ] = aux;
            i++;
            j--;
            
         }
         else{  // In case that they have the same points
            if( ranking[i] > ranking[j] ){
               aux = ranking[ i ];
               ranking[ i ] = ranking[ j ];
               ranking[ j ] = aux;
               i++;
               j--;
            }
            else{
               i++;
               j--;
            }
         }
     }
   }while( i <= j );
   if( esq < j ) QS( ranking, pple, esq, j );
   if( i < dret ) QS( ranking, pple, i, dret );
}


Since it comes from a modular program, I modified the code so that you can understand it easily. I beg you pardon if there is any error that can com from this.

Thank you very much in advance.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:47AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month