Thread: Sorting in descending order.

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    61

    Sorting in descending order.

    I have to write a function to sort a list of numbers into ascending and descending order. I've figured out how to do it in ascending order. So i thought that to do descending order, I would just have to switch the sign from greater than, to less than but it doesn't work. After the first element it stops.

    Code:
    void sort(float b[], int size, int direction)
    
    {
        float tmp;
        int k,lt;
        
        lt= size-1;
    
        
        if(direction == 1){  //ascending
                    
            for (k = 0; k < size; k++){                    
                if(b[k]>b[lt]){
                    tmp = b[i];
                    b[k] = b[lt];
                    b[lt] = tmp;
    
                }
            }
        }
            if(direction == 2){     //descending                           
                for (k = 0; k < size; k++){
                    if(b[k]<b[lt]){
                        tmp = b[k];
                        b[k] = b[lt];
                        b[lt] = tmp;
                    }
                }
            }
    }
    Last edited by november1992; 04-14-2012 at 08:46 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you post your actual code. I happily setup a source file to test your function, and when I tried to compile:
    Code:
    test.c: In function `sort':
    test.c:12: error: `a' undeclared (first use in this function)
    test.c:12: error: (Each undeclared identifier is reported only once
    test.c:12: error: for each function it appears in.)
    test.c:16: error: `i' undeclared (first use in this function)
    test.c:23: error: `c' undeclared (first use in this function)
    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
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Where are a and c declared? Or do you mean 'a' and 'c'?

    And there's no way your ascending sort will work either. Both are wrong.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    -100.000000
    1.000000
    2.000000
    3.000000
    4.000000
    5.000000
    7.000000
    24.000000
    9.000000
    -4.000000
    29.000000
    Last edited by november1992; 04-14-2012 at 09:07 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    november1992, this is what I tried to test with:
    Code:
    #include <stdio.h>
    
    void sort(float b[], int size, int direction)
     
    {
        float tmp;
        int k,lt;
         
        lt= size-1;
     
         
        if(direction == a){  //ascending
                     
            for (k = 0; k < size; k++){                   
                if(b[k]>b[lt]){
                    tmp = b[i];
                    b[k] = b[lt];
                    b[lt] = tmp;
     
                }
            }
        }
            if(direction == c){     //descending                          
                for (k = 0; k < size; k++){
                    if(b[k]<b[lt]){
                        tmp = b[k];
                        b[k] = b[lt];
                        b[lt] = tmp;
                    }
                }
            }
    }
    
    void print(float numbers[], int num)
    {
        int i;
        for (i = 0; i < num; ++i)
        {
            printf("%f ", numbers[i]);
        }
        putchar('\n');
    }
    
    int main(void)
    {
        float numbers[] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9};
        int num = sizeof(numbers) / sizeof(numbers[0]);
        print(numbers, num);
        sort(numbers, num, 'd');
        print(numbers, num);
        sort(numbers, num, 'c');
        print(numbers, num);
        return 0;
    }
    Notice how simple is the part that is used to test? No need for file I/O; file I/O can be added later. The whole point is to test the sort function, so I specify the test array in the code and just write to standard output.
    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

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    Quote Originally Posted by oogabooga View Post
    Where are a and c declared? Or do you mean 'a' and 'c'?

    And there's no way your ascending sort will work either. Both are wrong.
    Well, I didn't try negative numbers and it worked. It doesn't work when I put negative numbers in.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    Oh I almost forgot there has to be a swap function above the sort.

    Code:
    void swap(float *a, float *b)       
    {
    
        float tmp;
    
                    tmp = *a;                
                    *a = *b;               
                    *b = tmp;                
    
    
    
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by november1992
    Well, I didn't try negative numbers and it worked. It doesn't work when I put negative numbers in.
    Your code won't even compile for me, so I don't know what you are talking about.

    Anyway, oogabooga is right to say that your algorithm is wrong: you are just making one pass over the array. If it worked for you, that's because your input happens to be almost sorted in one direction, hence it did not work in the other direction.
    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

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    I'm not sure what that means, I guess I'll just ask my teacher. Thanks for trying.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by november1992
    I'm not sure what that means
    Go through your algorithm step by step. What does it actually do?

    Or, since you claim that you "figured out how to do it in ascending order", test by sorting in ascending order an array that is completely in descending order. Does it work?
    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

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by november1992 View Post
    I've figured out how to do it in ascending order.
    I'm afraid I have news for you...
    That code does not sort anything into asecnding order, partly because it does not compile, and partly because to actually perform a sort along the line of what you are doing would require nested loops.

    Thanks for trying.
    Don't be mistaken into thinking that we couldn't help you. In contrast, we actually know exactly how your code should look to work perfectly, It's just that we're trying to get you to see why what you've posted doesn't work at all, so you'll know what to fix.

    As it stands, your descending order code is actually a tiny bit closer to working than your ascending order code, because it doesn't have the glaring mistake of line 14. It's still not a working sorting algorithm though. Don't be fooled into thinking that one right output from the program means that everything is perfectly correct and working in any other case.
    Last edited by iMalc; 04-14-2012 at 02:10 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. three numbers in descending order
    By jackson6612 in forum C++ Programming
    Replies: 3
    Last Post: 05-21-2011, 08:05 AM
  2. Replies: 9
    Last Post: 04-01-2011, 04:13 PM
  3. ascending and descending order
    By ssk in forum C Programming
    Replies: 3
    Last Post: 03-25-2011, 08:03 PM
  4. Sorting in descending order
    By michael- in forum C Programming
    Replies: 3
    Last Post: 12-12-2005, 01:07 PM
  5. Sorting numbers in descending order
    By Yunasnk in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2003, 05:55 PM