Thread: Can't figure out how to swap

  1. #16
    Registered User
    Join Date
    Dec 2008
    Posts
    16
    i put a "WHOABUDDY" at the bottom of the swap function. it printed the list of names, WHOABUDDY twice, and then the list of names again still unsorted. the two whoabuddy's look odd to me as it is only called once. any thoughts?

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Twice does seem a bit low, yes. I'd expect as many swaps as you have iterations, which should be 5.

    Code:
       for (pass = 0; pass < SIZE - 1; pass++)
        {
          for (min = pass, trav = pass + 1; trav < SIZE; trav++)
             { 
                if (identity[trav].lName < identity[min].lName)
                   {
                      min = trav;
                   }
                trav++;
                       
             } 
          swap(&identity[pass], &identity[min]);
          pass++;
        }
    Do you actually want the red bit?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Registered User
    Join Date
    Dec 2008
    Posts
    16
    allllright, now we're gettin somewhere. i took out the pass++; and more whoabuddys came up but it still wasnt swapping. in the call of the swap function, i changed &identity[pass] to &identity[pass + 1] and it swapped only one of the names out of order, whichever one is at the top to its correct place.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by deathrattle View Post
    allllright, now we're gettin somewhere. i took out the pass++; and more whoabuddys came up but it still wasnt swapping. in the call of the swap function, i changed &identity[pass] to &identity[pass + 1] and it swapped only one of the names out of order, whichever one is at the top to its correct place.
    Then perhaps you should fix the other loop too - which has the same problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Registered User
    Join Date
    Dec 2008
    Posts
    16
    are you referring to the trav++; after the if? i'm playing around with all kinds of stuff and cannot figure out which section of the loop you are referring to.

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, you have two trav++ in your for-loop, one in the for-statement itself, and one just before the end brace. Whilst this is within the C language specification, I'm 99.9% sure that you do not want to do that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #22
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    hen you use a for loop, you don't have to iterate the variable inside the loop, the setup of the loop does that for you

    Code:
    for (var = 0; var < MAX; var++) { // Note the var++ here!
      // Do Something
      // No var++; needed!!!
    }
    If you used a while loop then you'd need to increment tha variable inside the loop...

    Code:
    var = 0;
    while (var < MAX) {
      // Do Something
      var++;
    }
    These two loops are exactly the same functionally.

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bladactania View Post
    hen you use a for loop, you don't have to iterate the variable inside the loop, the setup of the loop does that for you

    Code:
    for (var = 0; var < MAX; var++) { // Note the var++ here!
      // Do Something
      // No var++; needed!!!
    }
    If you used a while loop then you'd need to increment tha variable inside the loop...

    Code:
    var = 0;
    while (var < MAX) {
      // Do Something
      var++;
    }
    These two loops are exactly the same functionally.
    I agree - almost every time a for-loop-variable is changed outside of the for-statement, it is in error.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #24
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Generally for loops are used when you want to perform the same action a set number of times (like you do in your code). Generally while loops are used when you want to continue to perform a given action until a certain result is obtained. It is a fine, yet clear, distinction.

  10. #25
    Registered User
    Join Date
    Dec 2008
    Posts
    16
    i removed the second trav++; and it is still only swapping the top name that is out of order to its correct spot.

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is what your sort function should look like:
    Code:
       int pass, trav;
       
       for (pass = 0; pass < SIZE - 1; pass++)
       {
          for (trav = pass + 1; trav < SIZE; trav++)
          { 
             if (identity[pass].lName > identity[trav].lName)
             {
                swap(&identity[pass], &identity[trav]);                     
             }
          } //end of inner for
       }  //end of outer for
    You never need to increment pass, trav, or anything else, outside the for loops.
    Min is not needed at all.

    This is my favorite flavor of bubblesort. Please don't muck around with it.

    If you have a lot of empty array slots, they will be the first things you see after the sort, but that doesn't mean the sort is not working.

  12. #27
    Registered User
    Join Date
    Dec 2008
    Posts
    16
    i tried your code and it didn't swap any at all. im trying to do it with a selection sort instead of bubble if that is at all possible.

  13. #28
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by deathrattle View Post
    i tried your code and it didn't swap any at all. im trying to do it with a selection sort instead of bubble if that is at all possible.
    Post your code up. I've used that particular version of sorting for almost three decades

    It's selection sort, (sort of), but it sweeps all the way through the array on the inner loop, so it falls into the performance of the bubblesort "bunch".

    If you want selection sort, that's fine.

    You know you have to compare strings in C with strcmp(), right? You can't just compare name to name, directly.
    Last edited by Adak; 04-14-2009 at 01:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list swap function
    By Axel in forum C Programming
    Replies: 32
    Last Post: 01-16-2011, 09:40 AM
  2. Atomic compare and swap.
    By Maz in forum C Programming
    Replies: 3
    Last Post: 04-07-2009, 02:47 PM
  3. using swap to make assignment operator exception safe
    By George2 in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2008, 06:32 AM
  4. Double Link List Swap function
    By kennny2004 in forum C++ Programming
    Replies: 1
    Last Post: 04-12-2006, 11:52 AM