Thread: Help With Bubble Sort

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    19

    Help With Bubble Sort

    Okay, I'm back again. From what I understand this bubble sort, should sort the entire struct in ascending order according to the price member... But it just prints out the struct in its original form for me. Probably just a stupid mistake, but can anyone point it out?

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    #define NUM 5
    
    struct inv {
        char partID[6];
        int quantity;
        float price;
    };
    
    void invSort(struct inv items[NUM]);
    void invPrint(struct inv items[NUM]);
    
    main() 
    {
        struct inv items[NUM] = {{"HDWR9", 13, 4.53},
                                 {"WDGT4", 2, 15.82},
                                 {"POPL0", 4, 8.32},
                                 {"RTYN6", 7, 5.40},
                                 {"LOUN4", 61, 7.43}};
         
        printf("Inventory\n\n");
        invSort(items);
        invPrint(items);
         
        printf("\n\nPress any key to continue...");
        getch();
        return 0;
    } 
    
    void invSort(struct inv items[NUM])
    {
        int inner, outer;
        struct inv temp;
        int didSwap;
        
        for (outer = 0; outer < (NUM - 1); outer++) {
        didSwap = 0;
            for (inner = outer; inner < NUM; inner++) {
                if (items[outer].price > items[inner].price) {
                temp = items[outer];
                items[outer] = items[inner];
                items[inner] = temp;
                didSwap = 1;
                }
            }
            if (!didSwap) {
            break;
            }
        }
        return;
    }
    
    void invPrint(struct inv items[NUM]) 
    {
        int ctr = 0;
        
        for (ctr = 0; ctr < NUM; ctr++) {
        printf("Part ID: %s, Quantity: %d, Price: $%6.2f\n",
        items[ctr].partID, items[ctr].quantity, items[ctr].price);
        }
        return;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Compare and contrast:
    Code:
    void invSort(struct inv items[NUM])
    {
      int inner, outer;
      struct inv temp;
      int didSwap;
    
      for (outer = NUM - 1; outer > 0; outer--) {
        didSwap = 0;
        for (inner = 0; inner < outer; inner++) {
          if (items[inner].price > items[inner + 1].price) {
            temp = items[inner];
            items[inner] = items[inner + 1];
            items[inner + 1] = temp;
            didSwap = 1;
          }
        }
        if (!didSwap) {
          break;
        }
      }
      return;
    }
    Write down the execution of both your version and mine to see how they work. Nothing beats manual tracing like that for understanding an algorithm.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    19
    Gah, this book is upsetting me. That code is almost exact, word for word from the book. I don't understand why it would be wrong!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That code is almost exact, word for word from the book. I don't understand why it would be wrong!
    This isn't by any chance a book by Herbert Schildt would it?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    19
    Umm, nope, it's this one...
    http://www.amazon.com/exec/obidos/tg...glance&s=books

    I also have this one, but it's quite complex so I'm learning all the basics before i dive into it:
    http://www.amazon.com/exec/obidos/tg...glance&s=books
    Last edited by Explicit; 05-27-2004 at 12:38 PM.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    19
    Hmm this works aswell:

    Code:
    void invSort(struct inv items[NUM])
    {
        int inner, outer;
        struct inv temp;
        int didSwap;
        
        for (outer = (NUM -1); outer >= 0; outer--) {
        didSwap = 0;
            for (inner = 1; inner <= outer; inner++) {
                if (items[inner - 1].price > items[inner].price) {
                temp = items[inner - 1];
                items[inner - 1] = items[inner];
                items[inner] = temp;
                didSwap = 1;
                }
            }
            if (!didSwap) {
            break;
            }
        }
        return;
    }

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    22

    books!

    Quote Originally Posted by Explicit
    Umm, nope, it's this one...
    http://www.amazon.com/exec/obidos/tg...glance&s=books

    I also have this one, but it's quite complex so I'm learning all the basics before i dive into it:
    http://www.amazon.com/exec/obidos/tg...glance&s=books
    Buy Schaums outlines series especially B.S.Gottfried's "Programming with C". It is decent for beginners. Then you can move to K&R, the bible of C

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >It is decent for beginners.
    Decent for the price, but I wouldn't recommend it over better beginner's texts such as "Pointers on C" by Kenneth Reek.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bubble sort not working... wats d prob?
    By Huskar in forum C Programming
    Replies: 8
    Last Post: 03-31-2009, 11:59 PM
  2. My bubble sort only sorts once
    By Muller in forum C Programming
    Replies: 8
    Last Post: 03-27-2009, 04:36 PM
  3. Bubble Sort... which type?
    By gflores in forum C++ Programming
    Replies: 8
    Last Post: 08-15-2004, 04:48 AM
  4. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 04:54 PM
  5. optimizing bubble sort
    By Sargnagel in forum C Programming
    Replies: 14
    Last Post: 01-23-2003, 06:27 AM