Thread: Sort an Array in Ascending Order ?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    Sort an Array in Ascending Order ?

    I am trying to figure out how to sort an Array in Ascending order.

    The Array I want to sort is:

    array[4] // values: 1,3,2,4

    I have a example code but are not really sure. I do understand the code almost but I wonder where the stored array are.
    Is the sorted array simply: array ?
    Does this code make this array in ascending order ?

    Code:
    void exchange_sort(apvector <int> &array)
    {
         int i, j;
         int temp;   // holding variable
         int arrayLength = array.length( ); 
         for (i=0; i< (arrayLength -1); i++)    // to represent element to be compared
        {
              for(j = (i+1); j < arrayLength; j++)   // to represent the rest of the elements
             {
                    if (array[i] < array[j])
                   {
                            temp= array[i];          // swap
                            array[i] = array[j];
                            array[j] = temp;
                   }
              }
         }
         return;
    }
    Last edited by Coding; 01-09-2008 at 07:21 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    array is a reference. It is both the input array and the sorted array.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I understand.. so If I take the array out after running this code, the array will then be sorted, just to be sure this code works or if it is missing something...


    I think this code is for descending order. If you want to do it for ascending order, do you begin the counting from the top rather as for this example from the beginning of the array ?
    Last edited by Coding; 01-09-2008 at 07:30 PM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    usage would be like this:
    Code:
    apvector <int> myarray;
    
    //fill myarray
    exchange_sort(myarray);
    //my array is now sorted
    I think this code is for descending order. If you want to do it for ascending order, do you begin the counting from the top rather as for this example from the beginning of the array ?
    The <s>easiest</s> generic, usual way would be to use a reverse iterator.

    But I think to change the function, you would have to do this:
    Code:
    if (array[i] > array[j])
    disclaimer: untested.
    Last edited by robwhit; 01-09-2008 at 07:43 PM.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    okay.. thank you... I will try it out and see what I get...

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    or without reinventing the wheel, you'd do this:
    Code:
    #include <algorithm>
    #include <vector>
    
    int main()
    {
        std::vector<int> nums;
        ...
        std::sort( nums.begin(), nums.end() );
        ...
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. using qsort to sort an array of strings
    By bobthebullet990 in forum C Programming
    Replies: 6
    Last Post: 11-25-2005, 08:31 AM
  3. Divide and Conquer: array in the reverse order
    By Steamer in forum C Programming
    Replies: 11
    Last Post: 03-08-2004, 07:31 PM
  4. sorting arrays (ascending order)
    By utoots in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 08:57 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM