Friday 10 November 2017

C Program to sort array in ascending order


Code:

#include
#define MAX_SIZE 100

int main()
{
    int array[MAX_SIZE];
    int size;
    int i, j, temp;

    /* Input size of array */
    printf("Enter size of array: ");
    scanf("%d", &size);

    /* Input elements in array */
    printf("Enter elements in array: ");
    for(i=0; i
    {
        scanf("%d", &array[i]);
    }

    for(i=0; i
    {
        /* 
         * Place the currently selected element array[i]
         * to its correct place.
         */
        for(j=i+1; j
        {
            /* 
             * Swap if currently selected array element
             * is not at its correct position.
             */
            if(array[i] > array[j])
            {
                temp     = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }

    /* Print the sorted array */
    printf("\nElements of array in sorted ascending order: ");
    for(i=0; i
    {
        printf("%d\t", array[i]);
    }

    return 0;
}

Output:

Enter size of array: 10
Enter elements in array: 20 2 10 6 52 31 0 45 79 40

Elements of array in sorted ascending order: 0      2      6      10      20      31      40      45      52      79


More C Programs:












100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...