Thread: Has anyone seen anything like this?

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    2

    Has anyone had anything like this happen?

    I was practicing writing this code and couldn't figure out why it wasn't sorting the ID's. I compared it to what I had written before and saved to a text file and couldn't find any difference, except for the numbers I was using for the ID's. I tried using the numbers that I had used previously, and it worked. It doesn't make any sense. This is the set of ID's that work: 313, 453, 502, 101, 892, 475, 792, 912, 343, 633. I'm using MinGW btw.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main (void) {
    
        int ctr, tempID, left, right, didSwap;
        float tempBal;
    
    // It won't sort these ID's properly, but when I swap these ID's with the ones above, it sorts those properly.
        int custID[10] = {123, 456, 789, 987, 654, 321, 741, 852, 963, 147}; 
    
        float custBal[10] = {0.00, 98.74, 102.63, 54.26, 326.89, 0.00, 19.95, 220.63, 56.66, 150.55};
        
        for (left = 0; left < 9; left++) {
            didSwap = 0;
            for (right = left; right < 10; right++) {
                if (custID[right] < custID[left]) {
                    tempID = custID[right];
                    tempBal = custBal[right];
                    custID[right] = custID[left];
                    custBal[right] = custBal[left];
                    custID[left] = tempID;
                    custBal[left] = tempBal;
                    didSwap = 1;
                }
            }
            if (didSwap == 0) {
                break;
            }
        }
        for (ctr = 0; ctr < 10; ctr++) {
            printf("%d, ", custID[ctr]);
        }
        return 0;
    }
    Last edited by Bigcat; 06-12-2014 at 08:15 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Which sort algorithm were you trying to implement? Looking at your code, you are basically checking to see that for the number in each position, none of the numbers to its right is less than it, otherwise you swap them. Doing it this way, you cannot break when there are no swaps because you still have to check the remaining positions. Why this set of input reveals the bug is that the very first element in the array is the smallest, hence none of the numbers to its right are less than it, thus there are no swaps on the very first iteration.

    Perhaps you were thinking of implementing a variant of bubble sort instead, in which case you can break when there are no swaps because no swaps implies that all the elements have been "bubbled" into place. Or, perhaps you were thinking of implementing selection sort, where you find say, the least element in the unsorted portion and do just one swap with the element in the current position on each iteration.
    Last edited by laserlight; 06-12-2014 at 09:57 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    2
    Ah, that makes sense. The first for loop doesn't get to iterate before breaking. I guess I didn't look at it as closely as I should have, since it worked properly the other times I ran it. The book it was from said it was supposed to be bubble sort, but it appears to work more like selection sort, atleast from the videos I've seen. Thanks for responding!

Popular pages Recent additions subscribe to a feed