Bubble Sort



  • Hallo,

    Wir haben die Aufgabe bekommen den Code sowohl syntaktisch als auch semantisch zu korrigieren. By the way der Part mit sizeof(int)*8 ist dafür gedacht dass es plattformunabh läuft, also auch 16 bit cpus usw.
    Ich hab die Bitcount funktion nicht verstanden.

    Hier der Original Code:

    #include <stdio.h> 
    
    static int bitcount(int num); 
    
    /* Bubble Sort */ 
    void sort(int *a, int size) { 
      int i, j; 
    
      for (i = size - 1; i > 0; i--) { 
        for (j = 1; j <= i; j++) { 
          if(bitcount(a[j-1] < bitcount(a[j])) 
            int temp = a[j-1]; 
            a[j-1] = a[j]; 
            a[j-1] = temp; 
        } 
      } 
      return 0; 
    } 
    
    /* Bitcount */ 
    static unsigned int bitcount(int num) { 
      unsigned int count; 
    
      while (num) { 
        count += num && 1; 
        num = num >> 1 
      } 
    
      return count; 
    } 
    
    static void printarray(int **a, int  size) 
    { 
     int j;  
    
     for(int i = 1; i <= size, i++) { 
       int num = a[i]; 
    
       for (j = 0; j < sizeof(int) * 8; i++) { 
         printf("%i", num & 1); 
         num = num >> 1; 
       } 
    
       print("\n"); 
    } 
    
    int main(int argc, String argv) { 
    
     int a[20] = { 
       1, 22, 46, 1993, 22222, 
       21, 2987, 675667, 7, 77, 
       65, 35133, 33, 13456, 678, 
       3, 0, 99, 3345677, 
     };   
     printarray(a, 20);  
     printf("\n");  
     sort(a, 20);  
     printarray(a, 20); 
     return 0;  
    }
    

    Hier meine korrigierte Version, die nur nullen ausgibt.

    #include <stdio.h>
    
    static int bitcount(int num);
    
    /* Bubble Sort */
    
    void sort(int *a, int size){
    int i, j;
    
    for(i = size-1; i > 0; i--){
      for(j = 1; j <= i; j++){
         if((bitcount(a[j-1]) < bitcount(a[j]))){
            int temp = a[j-1];
            a[j] = a[j-1];
            a[j-1] = temp;
         }
      }
    }
    }
    
    /* Bitcount */
    
    static int bitcount(int num){
    
    unsigned int count;
    
    while(num){
      count += num & 1;
      num = num >> 1;
    }
    return count;
    }
    
    static void printarray(int *a, int size){
    
     int j;
    
     for(int i = 1; i <= size; i++){
      int num = a[i];
    
      for(j = 0; j < sizeof(int)*8; i++){
         printf("%d", num & 1);
         num = num >> 1;
      }
     }
     printf("\n");
    }
    
    int main(int argc, char **argv){
    
    int a[19] = {1, 22, 46, 1993, 22222, 21, 2987, 675667, 7, 77, 65, 35133, 33, 13456, 678,   3, 0, 99, 3345677};
    
    printarray(a, 19);
    printf("\n");
    sort(a, 19);
    printarray(a, 19);
    return 0;
    }
    


  • ...


  • Mod

    Zeilen 13-15 deines Codes solltest du dir nochmal genau anschauen (das ist nicht der einzige Fehler, nur wahrscheinlich der, der vorrangig für deine Nullen verantwortlich ist).


  • Mod

    Xeno1987 schrieb:

    By the way der Part mit sizeof(int)*8 ist dafür gedacht dass es plattformunabh läuft, also auch 16 bit cpus usw.

    Was durch diese Aktion eben gerade verhindert wird. Denn bei 16-Bit chars würde sich die Funktion nun verrechnen, da die 8 fest einprogrammiert ist.

    Siehe CHAR_BITS:
    http://www.cplusplus.com/reference/climits/



  • Also ich hab Zeile 13-15 die Zeile 14 so abgeändert, damit der die Werte richtig vertauscht

    a[j-1] = a[j]
    

    , aber ich krieg immer noch die nullen.


  • Mod

    Zu viel Freizeit:

    #include <stdio.h>
    #include <limits.h>
    #include <stddef.h>
    
    /* Bitcount */
    
    static unsigned int bitcount(int num)
    { 
      unsigned int count = 0;
    
      while(num){
        count += num & 1;
        num = num >> 1;
      }
      return count;
    }
    
    /* Bubble Sort */
    
    void sort(int *a, size_t size)
    {
      size_t i, j;
    
      for(i = size-1; i > 0; i--){
        for(j = 1; j <= i; j++){
          if(bitcount(a[j-1]) < bitcount(a[j])){
            int temp = a[j-1];
            a[j-1] = a[j];
            a[j] = temp;
          }
        }
      }
    }
    
    static void printarray(int *a, size_t size)
    { 
      size_t i,j;
    
      for(i = 0; i < size; ++i)
        {
          int num = a[i];
    
          for(j = 0; j < sizeof(int)*CHAR_BIT; ++j)
            {
              printf("%d", num & 1);
              num = num >> 1;
            }
        }
      putchar('\n');
    }
    
    int main()
    { 
      int a[] = {1, 22, 46, 1993, 22222, 21, 2987, 675667, 7, 77, 65, 35133, 33, 13456, 678,   3, 0, 99, 3345677};
    
      size_t length = sizeof a/sizeof*a;
      printarray(a, length);
      putchar('\n');
      sort(a, length);
      printarray(a, length);
      return 0;
    }
    

    Beachte insbesondere den kleinen aber feinen Unterschied am Ende meiner Zeile 43 (bei dir Zeile 39). Der war an deinen Nullen schuld.

    Man könnte noch sehr, sehr viele Details verbessern, aber ich möchte es mal nicht übertreiben.



  • hey danke erstmal jetzt hab ich es auch verstanden. Die wollten gar nicht die zahlen ausgeben sondern die bits.


Anmelden zum Antworten