Selection Sort in descending order HELP!!

Discussion in 'C Programming' started by Clinto, Oct 21, 2007.

  1. Clinto

    Clinto Guest

    Hello,

    I am using the below code to sort an array in ascending order, I also
    need to sort in descending order. I cannot get figure out what has to
    change to make this happen. If someone could *point me in the right
    direction, it would be appreciated. pLast is (comes from another
    function) the largest element in the array and paAry is the array
    being passed

    /* ==================== selectSort ====================
    Sorts by selecting smallest element in unsorted
    portion of the array and exchanging it with element
    at the beginning of the unsorted list.
    Pre array must contain at least one item
    pLast is pointer to last element in array
    Post array rearranged smallest to largest
    */
    void selectSort (int* pAry, int* pLast)
    {
    // Local Declarations
    int* pWalker;
    int* pSmallest;

    // Statements
    for (pWalker = pAry; pWalker < pLast; pWalker++)
    {
    pSmallest = smallest (pWalker, pLast);
    exchange (pWalker, pSmallest);
    } // for
    return;
    } // selectSort



    /* ==================== smallest ====================
    Find smallest element starting at current pointer.
    Pre pAry points to first unsorted element
    Post smallest element identified and returned
    */
    int* smallest (int* pAry, int* pLast)
    {
    // Local Declarations
    int* pLooker;
    int* pSmallest;

    // Statements
    for (pSmallest = pAry, pLooker = pAry + 1;
    pLooker <= pLast;
    pLooker++)
    if (*pLooker < *pSmallest)
    pSmallest = pLooker;

    return pSmallest;
    } // smallest

    /* ====================== exchange ====================
    Given pointers to two array elements, exchange them
    Pre p1 & p2 are pointers to exchange values
    Post exchange is completed
    */
    void exchange (int* p1, int* p2)
    {
    // Local Declarations
    int temp;

    // Statements
    temp = *p1;
    *p1 = *p2;
    *p2 = temp;
    return;
    } // exchange
    Clinto, Oct 21, 2007
    #1
    1. Advertising

  2. Clinto said:

    > Hello,
    >
    > I am using the below code to sort an array in ascending order, I also
    > need to sort in descending order.


    By "ascending order" you mean "smallest first". Think about how the code
    determines which is smallest. Can you persuade it to change its mind?
    Perhaps by using an operator other than < ? Or maybe by switching a couple
    of operands around?

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Oct 21, 2007
    #2
    1. Advertising

  3. Clinto

    Army1987 Guest

    On Sun, 21 Oct 2007 00:10:27 -0700, Clinto wrote:

    > Hello,
    >
    > I am using the below code to sort an array in ascending order, I also
    > need to sort in descending order. I cannot get figure out what has to
    > change to make this happen. If someone could *point me in the right
    > direction, it would be appreciated.

    Have you tried to do the most obvious thing to do, namely writing
    a greatest() function identical to smallest() except using a > in
    the place of a <, and using it instead of smallest()? If so, what
    was wrong with the results you got? If not, did you bother to try
    and figure out how those functions work?
    --
    Army1987 (Replace "NOSPAM" with "email")
    A hamburger is better than nothing.
    Nothing is better than eternal happiness.
    Therefore, a hamburger is better than eternal happiness.
    Army1987, Oct 21, 2007
    #3
  4. Clinto

    Clinto Guest

    On Oct 21, 3:34 am, Army1987 <> wrote:
    > On Sun, 21 Oct 2007 00:10:27 -0700, Clinto wrote:
    > > Hello,

    >
    > > I am using the below code to sort an array in ascending order, I also
    > > need to sort in descending order. I cannot get figure out what has to
    > > change to make this happen. If someone could *point me in the right
    > > direction, it would be appreciated.

    >
    > Have you tried to do the most obvious thing to do, namely writing
    > a greatest() function identical to smallest() except using a > in
    > the place of a <, and using it instead of smallest()? If so, what
    > was wrong with the results you got? If not, did you bother to try
    > and figure out how those functions work?
    > --
    > Army1987 (Replace "NOSPAM" with "email")
    > A hamburger is better than nothing.
    > Nothing is better than eternal happiness.
    > Therefore, a hamburger is better than eternal happiness.


    I did make a new selectsort() function and similar function to
    smallest() which i called largest() and switched the <. I made changes
    in the selectsort function as well. (maybe that is my problem) My
    output was the same as what was passed to it. So I know I am doing
    somehthing incorrectly. I see what the function does to get the
    smallest items in the array and sort them in ascending order. I just
    do not quite understand how to change it to descending order. I am
    sure you can tell this is a homework assignment so I am not asking
    anyone to do it for me.

    I just need some help as I have spent too many hours on it already.
    Thanks for all input, I will work with the suggestions given thus far.
    I will rewrite the functions later today. I have church this morning.
    Clinto, Oct 21, 2007
    #4
  5. Clinto

    Clinto Guest

    On Oct 21, 9:12 am, Clinto <> wrote:
    > On Oct 21, 3:34 am, Army1987 <> wrote:
    >
    >
    >
    >
    >
    > > On Sun, 21 Oct 2007 00:10:27 -0700, Clinto wrote:
    > > > Hello,

    >
    > > > I am using the below code to sort an array in ascending order, I also
    > > > need to sort in descending order. I cannot get figure out what has to
    > > > change to make this happen. If someone could *point me in the right
    > > > direction, it would be appreciated.

    >
    > > Have you tried to do the most obvious thing to do, namely writing
    > > a greatest() function identical to smallest() except using a > in
    > > the place of a <, and using it instead of smallest()? If so, what
    > > was wrong with the results you got? If not, did you bother to try
    > > and figure out how those functions work?
    > > --
    > > Army1987 (Replace "NOSPAM" with "email")
    > > A hamburger is better than nothing.
    > > Nothing is better than eternal happiness.
    > > Therefore, a hamburger is better than eternal happiness.

    >
    > I did make a new selectsort() function and similar function to
    > smallest() which i called largest() and switched the <. I made changes
    > in the selectsort function as well. (maybe that is my problem) My
    > output was the same as what was passed to it. So I know I am doing
    > somehthing incorrectly. I see what the function does to get the
    > smallest items in the array and sort them in ascending order. I just
    > do not quite understand how to change it to descending order. I am
    > sure you can tell this is a homework assignment so I am not asking
    > anyone to do it for me.
    >
    > I just need some help as I have spent too many hours on it already.
    > Thanks for all input, I will work with the suggestions given thus far.
    > I will rewrite the functions later today. I have church this morning.- Hide quoted text -
    >
    > - Show quoted text -


    I re-wrote the sort functions and finally got it to work:

    5 numbers read.

    Your data sorted are:

    Ascending Original Descending

    14 26 57
    26 14 41
    33 57 33
    41 33 26
    57 41 14

    End of List

    Thanks for the assistance.
    Clinto, Oct 21, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Scheaffer via JavaKB.com

    Descending Order Problem

    Robert Scheaffer via JavaKB.com, Nov 26, 2004, in forum: Java
    Replies:
    1
    Views:
    1,855
    Andrew Thompson
    Nov 26, 2004
  2. Luigi Napolitano

    Sort in descending order with index

    Luigi Napolitano, Dec 17, 2004, in forum: C++
    Replies:
    2
    Views:
    3,096
    Luigi Napolitano
    Dec 17, 2004
  3. Luigi Napolitano

    Sort in the Descending Order

    Luigi Napolitano, Dec 17, 2004, in forum: C Programming
    Replies:
    7
    Views:
    378
  4. sudhir
    Replies:
    8
    Views:
    827
    Kenny McCormack
    Dec 6, 2005
  5. , India

    qsort descending order

    , India, Mar 19, 2007, in forum: C Programming
    Replies:
    5
    Views:
    678
    Richard Tobin
    Mar 19, 2007
Loading...

Share This Page