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've got an assignment that requires me to use selection sort in descending order, and here's my code so far: ...

  1. #1
    null
    Join Date
    Dec 2005
    Posts
    18

    Sorting in descending order

    I've got an assignment that requires me to use selection sort in descending order, and here's my code so far:
    Code:
    void SortRecord( float grades[] )
    {
        int i, j;
        int min, temp;
    
        for( i = 0; i < (STUDENT_RECORDS - 1); i++ )
        {
            min = i;
    
            for( j = (i + 1); j < STUDENT_RECORDS; j++ )
            {
                if( grades[j] < grades[min] )
                    min = j;
            }
    
            temp = grades[i];
    
            grades[i] = grades[min];
            grades[min] = temp;
        }
    
        for( i = 0; i < STUDENT_RECORDS; i++ )
        {
            printf( "%.1f\n", grades[i] );
        }
    
        printf( "\n" );
    }
    This works grand for sorting in ascending order, but I dont know what to change to get it to go the other way.

    I'm still working on grasping the flow of logic and code here, which is why I'm not simply using trial and error to solve the problem. Any help is appreciated. Thanks

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    You seem to have more variables than you need there and your overdeclaration kinda makes the code confusing. There are lots of standard sorting techniques out there, some are complex, but fast, others are simple but slow.

    Here is a simple one:

    Code:
    for (int x = MAX; x > 0; x--) {
        for(int i = 0; i < x; i++) {
           if (array[i] > array[x])
              swap(array[i], array[x]);
        }
    }
    This kind of sort is close to what you have, you just did a little more than you need. With setups like this, switching from ascending to descending should be as simple as changing the greater than sign to a less than sign in the if statement. Also, I say this is slow, but it should be more than fast enough to accomidate your program, I'm sure.

    If you want to look into sorting algorithms, the cprogramming.com has a few. For alot more detail on them, you could go to: http://eternallyconfuzzled.com/tuts/sorting.html
    Last edited by SlyMaelstrom; 12-12-2005 at 02:18 PM.
    Sent from my iPadŽ

  3. #3
    null
    Join Date
    Dec 2005
    Posts
    18
    I did check out the sorting tutorials on cprogramming.com, but I found them to be very unhelpful (my comprehension level is below that of someone who will make it in the programming world, but I'ma try it anyway ).

    Thanks for your help. I think I see how it works, and am going to go give it my best.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I dont know what to change to get it to go the other way.
    For the less sophisticated sorts, changing from ascending to descending is usually a simple matter of flipping the comparison. In your function, you change this:
    Code:
    if( grades[j] < grades[min] )
    to this:
    Code:
    if( grades[j] > grades[min] )
    and you're done. Though it couldn't hurt to change the variable name min to max, or you might confuse people.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting numbers in descending order
    By Yunasnk in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2003, 05:55 PM
  2. Sorting in alphabetical order
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 08-24-2003, 10:07 AM
  3. Sorting in Alphabetical order
    By Andre Santiago in forum C Programming
    Replies: 1
    Last Post: 12-13-2002, 06:14 PM
  4. Replies: 1
    Last Post: 01-06-2002, 06:28 PM
  5. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 02:29 PM

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