Thread: Sorting an array

  1. #1
    Registered User
    Join Date
    Sep 2007

    Sorting an array

    I have looked through the forums for a problem similar to mine but given the specifics of my particular assignment, none of the posts (that I've seen so far) have similar structures.

    I have an array of a given length... in this case, we'll say:


    using a function named larger(a, b) in another.c file, which determines which of two ints is larger, how do I sort the elements of an array in ascending order? I've been sitting here for hours messing with it but I keep confusing myself.

    This is what I have so far, which is way off, but just to show you the code structure for this part of the code.

            for(j = total; j > 0; j--)  /* total is the length of the array */
                    for(i = 0; i < j; i++)
                            temp = larger(input[i], input[j]);
                            if(temp = input[i])
                                    temp1 = input[j];
                                    input[j] = input[i];
                                    input[i] = temp1;
    Thank you in advance for taking time to look at this.

  2. #2
    Registered User
    Join Date
    Nov 2007
    I posted a qsort program a few days ago, under the heading Golf Clubs, Cash. There may be something there to help you.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Whilst I recommend going with qsort instead of rolling your own sorting algorithm, there are a few things you should learn with respect to the code you have written.

    1. There is a buffer overrun. The first loop start with j equal to total, which is the length of the array, so item j does not exist. j would need to start at length-1.
    2. Comparison uses ==, not =. This line would not do what you wanted:
    if(temp = input[i])
    3. a comparison function such as your 'larger' function only needs to return a BOOL indicating IF the first value is larger than the second. It doesn't actually need to return the value of which one is larger. You've probably written more of a 'max' function.
    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. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  2. two dimensional array sorting in C/C++
    By George2 in forum C Programming
    Replies: 16
    Last Post: 11-19-2006, 03:17 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM