Thread: Array/Structure Bubble Sort

  1. #1
    Registered User JKI's Avatar
    Join Date
    Sep 2003
    Posts
    19

    Array/Structure Bubble Sort

    I have an array of structures which I am attempting to bubble sort.

    My problem is I cannot get a "temp" struct to hold one of the structures within the array for the sort.

    My data is valid within the array, but after the sort I am getting junk output.

    Is there an "easy" way to do this?

    Here's the sort currently:

    Code:
    /* FYI:
     
     struct key
     {
      char invnbr[16];
      int recnbr;
     };
     key *keyptr = new key[12];
     key tempkey; */ 
    
    
    while ((k < rrn - 1) && exchangeMade)
     {
      exchangeMade = false;
      ++k;
    
      for (j = 0; j < rrn - k; ++j)
      if (keyptr[j].invnbr > keyptr[j + 1].invnbr)
      {
       tempkey = keyptr[j];
       keyptr[j] = keyptr[j + 1];
       keyptr[j + 1] = tempkey;
       exchangeMade = true;
      }
     }
    I have also tried to pass each member of the struct as well to no avail. (eg: tempkey.member = keyptr[j].member).

    I'm seriously confused, any pointers (no pun) appreciated!
    "Only two things are infinite, the universe and human stupidity, and I'm not sure about the former."-Einstein

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    You're not comparing elements properly.

    Code:
    if (keyptr[j].invnbr > keyptr[j + 1].invnbr)
    What you're doing there is comparing memory addresses, because invnbr by itself is the address of the array. You need to take a look at the strcmp()/lstrcmp() and strcmpi()/lstrcmpi() functions. Everything should work fine then.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User JKI's Avatar
    Join Date
    Sep 2003
    Posts
    19
    Originally posted by bennyandthejets
    You're not comparing elements properly.

    Code:
    if (keyptr[j].invnbr > keyptr[j + 1].invnbr)
    What you're doing there is comparing memory addresses, because invnbr by itself is the address of the array. You need to take a look at the strcmp()/lstrcmp() and strcmpi()/lstrcmpi() functions. Everything should work fine then.

    Thanks! Hope you're right, it was making me nuts wondering why one structure couldn't hold the other.

    I'll give it shot and let you know if I got it.

    Thanks again, sometimes another set of eyes is a lifesaver.

    ::Jazz
    "Only two things are infinite, the universe and human stupidity, and I'm not sure about the former."-Einstein

  4. #4
    Registered User JKI's Avatar
    Join Date
    Sep 2003
    Posts
    19

    Thumbs up

    Benny,

    You were entirely correct.

    Changed it to:

    Code:
    if (strcmp(keyptr[j].invnbr, keyptr[j + 1].invnbr) > 0)
    ... and it works fine.

    Now on to the next crisis. LOL. (I'm firmly blinded by code and facing the "deadline!")

    Thanks again.

    ::Jazz
    "Only two things are infinite, the universe and human stupidity, and I'm not sure about the former."-Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I bubble sort alphabetically?
    By arih56 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2008, 02:30 AM
  2. help with debug (bubble sort algorithm)
    By lbraglia in forum C Programming
    Replies: 5
    Last Post: 10-19-2007, 05:24 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