Thread: Sorting bug

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    Sorting bug

    Hi. At College we've been asked to make an application in C that sort a card game. No big deal, but have made a sorting function that sorting strangely...
    It's a Bubblesort.

    Can you help me to fix it? Here's the code, sorry, but the name of variables are in french. I put in bold the bugging Bubblesort function.
    Thanks a lot.
    Code:
    #include <conio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <windows.h>
    
    #define AFFICHE 'A'
    #define BRASSE 'B'
    #define CLOCHE '\a'
    #define TRI 'T'
    #define FIN 27
    
    enum Couleur {Coeur=3, Carreau, Trefle, Pique};
    enum Valeur {Deux=2, Trois, Quatre, Cinq, Six, Sept, Huit, Neuf, Dix, Valet, Dame, Roi, As};
    struct Carte
    {
      enum Valeur valeur ;
      enum Couleur couleur ;
    };
    
    void fullscreen(void);
    void initialiseJeu(struct Carte jeu[]);
    void afficheJeu(struct Carte jeu []);
    void brasseJeu(struct Carte jeu []);
    void triJeu(struct Carte jeu []);
    int menu(void);
    
    //struct Carte carte, jeuCarte[52];
    //enum Valeur valeur;
    //enum Couleur couleur;
    
    int main(int argc, char* argv[])
    {
      struct Carte jeuCarte[52];
      int choix;
    
      fullscreen();
      randomize();
      initialiseJeu(jeuCarte);
      afficheJeu(jeuCarte);
      do
      {
        choix = menu();
        switch(choix)
        {
          case AFFICHE  : putch(choix);
                          getch();
                          afficheJeu(jeuCarte);
                          getch();
                          break;
          case BRASSE   : putch(choix);
                          getch();
                          brasseJeu(jeuCarte);
                          break;
          case TRI      : putch(choix);
                          getch();
                          triJeu(jeuCarte);
                          break;
          case FIN      : break;
          default       : putch(CLOCHE);
        }
      } while (choix != FIN);
      //system("pause");
      return 0;
    
    }
    void initialiseJeu(struct Carte jeu[])
    {
      int i=0;
      enum Couleur c;
      enum Valeur v;
      for (c=Coeur;c<=Pique;c=c+1)
      {
        for (v=Deux;v<=As;v=v+1)
        {
        jeu[i].couleur=c;
        jeu[i].valeur=v;
        i++;
        }
      }
    }
    void afficheJeu(struct Carte jeu [])
    {
      int i=0;
      enum Couleur c;
      enum Valeur v;
      clrscr();
      for (c=Coeur;c<=Pique;c=c+1)
      {
        for (v=Deux;v<=As;v=v+1)
        {
    /*    if(jeu[i].couleur==Coeur || jeu[i].couleur==Carreau)
            textcolor(RED);
          else
            textcolor(BLACK); */
          switch(jeu[i].valeur)
          {
            case Valet  : cprintf("%3c",'J');
                          break;
            case Dame   : cprintf("%3c",'D');
                          break;
            case Roi    : cprintf("%3c",'K');
                          break;
            case As     : cprintf("%3c",'A');
                          break;
            default     : cprintf("%3d",jeu[i].valeur);
                          break;
          }
          cprintf("%c",jeu[i].couleur);
          i++;
        }
        cputs("\n\r");
      }
    }
    void brasseJeu(struct Carte jeu [])
    {
      struct Carte c;
      int i, j;
    
      for (i=0;i<52;i++)
      {
        j=random(52);
        c = jeu[i];
        jeu[i] = jeu[j];
        jeu[j] = c;
      }
    }
    
    void triJeu(struct Carte jeu [])
    {
        int i   = 0;
        int j   = 0;
        int tmp = 0;
        int permut;
    
        permut = 1;
        for(i = 0 ; (i < 52) && permut==1; i++)
        {
            permut = 0;
            for(j = 1 ; j < 52 ; j++)
            {
                if(jeu[j].couleur < jeu[j-1].couleur)
                {
                    tmp = jeu[j-1].couleur;
                    jeu[j-1].couleur = jeu[j].couleur;
                    jeu[j].couleur = tmp;
                    permut = 1;
                }
                else if(jeu[j].valeur < jeu[j-1].valeur)
                {
                    tmp = jeu[j-1].valeur;
                    jeu[j-1].valeur = jeu[j].valeur;
                    jeu[j].valeur = tmp;
                    permut = 1;
                }
    
            }
        }
    }
    
    int menu(void)
    {
      int touche;
      clrscr();
      //textbackground(BLUE);
      //textcolor(RED);
      gotoxy(33,5);
      cputs("Jeu de cartes");
      gotoxy(25,10);
      cputs("Affiche le jeu de cartes");
      gotoxy(25,15);
      cputs("Brasse le jeu de cartes");
      gotoxy(25,20);
      cputs("Tri le jeu de cartes");
      gotoxy(25,25);
      cputs("Quitter le jeu de cartes");
      cputs("(Esc)");
      gotoxy(35,40);
      cputs("Choix: ");
      touche = toupper(getch());
      return touche;
    }
    void fullscreen(void)
    {
      keybd_event(VK_MENU,0x38,0,0);
      keybd_event(VK_RETURN,0x1c,0,0);
      keybd_event(VK_RETURN,0x1c,KEYEVENTF_KEYUP,0);
      keybd_event(VK_MENU,0x38,KEYEVENTF_KEYUP,0);
      window(0,0,80,50);
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    well, for loop dont do what it should suppose to do for the bubblesort
    Code:
    for(i = 0 ; (i < 52) && permut==1; i++)
        {
            permut = 0;
            for(j = 1 ; j < 52 ; j++)
            {
                if(jeu[j].couleur < jeu[j-1].couleur)
                {
    this should be something like this
    Code:
    for(i = 0 ; (i < 52-1) && permut==1; i++)
        {
            permut = 0;
            for(j = i+1 ; j < 52 ; j++)
            {
                if(jeu[i].couleur < jeu[j].couleur)
                {
    j should be 1 morer than the i so that j wont check the same value of what the i has. for example

    [5] [3] [8] [2] <-- if this is your list and the ith element is the first element that is [5] the jth should be pointing to rge i+1 that us [3] and then compare those two and if needed swap the elements. in th above case u need to swap

    ssharish2005

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    Dont' seems to work...Thanks.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Something like this:
    Code:
    for(i = 51; i >= 0; i--) {
       for(j = 0 ; j < i ; j++) {
          if(jeu[j].couleur < jeu[i].couleur) {
              tmp = jeu[i].couleur;
              jeu[i].couleur = jeu[j].couleur;
              jeu[j].couleur = tmp;
          }
          else if(jeu[j].valeur < jeu[i].valeur) {
              tmp = jeu[i].valeur;
              jeu[i].valeur = jeu[j].valeur;
              jeu[j].valeur = tmp;
          }
       }
    }
    Though this might be considered a selection sort. I tend to mix those two up. I think the real bubble sort is more like this:
    Code:
    for(i = 0; i < 52; i++) {
       permut = 1;
       for(j = 51; j > 0; j--) {
          if(jeu[j].couleur < jeu[j-1].couleur) {
             tmp = jeu[j-1].couleur;
             jeu[j-1].couleur = jeu[j].couleur;
             jeu[j].couleur = tmp;
             permut = 0;
          }
          else if(jeu[j].valeur < jeu[j-1].valeur) {
              tmp = jeu[j-1].valeur;
              jeu[j-1].valeur = jeu[j].valeur;
              jeu[j].valeur = tmp;
              permut = 0;
          }
       }
       if (permut) break;
    }
    With the flag as you had.
    Last edited by SlyMaelstrom; 03-23-2006 at 02:25 PM.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    The first thing you show me give me something like that :

    http://img136.imageshack.us/img136/5082/sort8wt.jpg

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    Forget about it, a classmate had found the solution.

    Code:
    void triJeu(struct Carte jeu [])
       {
       int i   = 0;
            int j   = 0;
            enum Couleur tmp;
            int tm;
            int permut;
    
            permut = 1;
            for(i = 0 ; (i < 52) && permut==1; i++)
            {
                    permut = 0;
                    for(j = 1 ; j < 52 ; j++)
                    {
    
                            if(jeu[j].couleur < jeu[j-1].couleur)
                            {
                                    tmp = jeu[j-1].couleur;
                                    jeu[j-1].couleur = jeu[j].couleur;
                                    jeu[j].couleur = tmp;
                                    tm = jeu[j-1].valeur;
                                    jeu[j-1].valeur = jeu[j].valeur;
                                    jeu[j].valeur = tm;
                                    permut = 1;
                                 }
                    }
            }
            permut = 1;
           for (i = 13;i<53;i=i+13){
    
            do {
            permut = 1;
                    for (j=(i-13);j<(i-1);j++){
                     if(jeu[j].valeur > jeu[j+1].valeur)
                            {
                                    tm = jeu[j].valeur;
                                    jeu[j].valeur = jeu[j+1].valeur;
                                    jeu[j+1].valeur = tm;
                                    permut = 0;
                            }
                    }
            } while (!permut);
           }
    
    }
    See ya next time.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    So basically it's the same bubble sort I gave you except you wanted to swap both when you found a mismatch. Something I probably could have figured had it been in english.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 07:52 AM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  4. Sorting words with a fast, effincient sorting method
    By Unregistered in forum C++ Programming
    Replies: 19
    Last Post: 07-12-2002, 04:21 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM