Like Tree1Likes
  • 1 Post By iMalc

Sorting in descending order.

This is a discussion on Sorting in descending order. within the C Programming forums, part of the General Programming Boards category; I have to write a function to sort a list of numbers into ascending and descending order. I've figured out ...

  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
    20,981
    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)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,981
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,981
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,981
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,269
    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.
    Salem likes this.
    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, 12:07 PM
  5. Sorting numbers in descending order
    By Yunasnk in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2003, 04:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21