Monday, 13 November 2017

C Program to Sort the Array in an Ascending Order


Code:

#include   stdio.h
#define MAX 100
int main()
{
    int arr[MAX],n,i,j;
    int temp;
     
    printf("Enter total number of elements: ");
    scanf("%d",&n);
     
    //read array elements
    printf("Enter array elements:\n");
    for(i=0;i< n;i++)
    {
        printf("Enter element %d: ",i+1);
        scanf("%d",&arr[i]);
    }
     
    //sort array
    for(i=0;i< n;i++)
    {
        for(j=i+1;j< n;j++)
        {
            if(arr[i]>arr[j])
            {
                temp    =arr[i];
                arr[i]  =arr[j];
                arr[j]  =temp;
            }
        }
    }
     
    printf("\nArray elements after sorting:\n");
    for(i=0;i< n;i++)
    {
        printf("%d\n",arr[i]);
    }
    return 0;
}


Output:

Enter total number of elements: 5
    Enter array elements:
    Enter element 1: 100
    Enter element 2: 999
    Enter element 3: 200
    Enter element 4: 800
    Enter element 5: 300

    Array elements after sorting:
    100
    200
    300
    800
    999


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...