One Way Swapping Sort

Code Id 44
Date Updated 11/7/2010
Title One-way swapping sort  
Description
The program sorts a series of numbers. The sorting method used is to repetitively swap list items, the
 higher ones to the end, the lowers to
 the beginning until the list is sorted. 
                                  
Codes Snippet
// *****************************************
// Title:     "One-way swapping sort"
// 
// Description:
//  The program sorts a series of numbers.
//  The sorting method used is to
//  repetitively swap list items, the
//  higher ones to the end, the lowers to
//  the beginning until the list is sorted.
// *****************************************

#include 

void SwapMembers (int items[], int index1, int index2)
{
        int temp;
        temp=items[index1];
        items[index1]=items[index2];
        items[index2]=temp;
}

main ()
{
        int n,m;
    int numbers [] = {102,21,83,42,11,10,9,3,20,27,15,92,2};

        // now a trick to get the number of items in an ARRAY:
        // divide the size of the array by the size of one of its members:
        const int size = sizeof(numbers) / sizeof(numbers[0]);

        // n: specifies the location in the array that has to be adequately set,
        //    starting from the end (the greater)
        // m: each iteration of m stores in position n the greater number found
        //    form position 0 to position n.
        for (n=size-1; n>0; n--)
                for (m=0; m numbers[m+1] )
                                SwapMembers (numbers, m, m+1 );

        // Output the sequence
        for (n=0;n<< numbers[n] << ", ";

        return 0;
}

Comments are closed.