Monday, February 15, 2016

C Program to sort an integer array in ascending order.

/*=================================================================================
 **
 **  File Name     : sortArray.c
 **  Creation Date : Mon 15 Feb 2016 04:43:08 AM IST
 **  Last Modified : Mon 15 Feb 2016 05:06:09 AM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.in@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/
#include <stdio.h>
int main()
{
    printf("\n= = = = = Sort the Array elements in ascending order = = = = =\n");
    int i, j, a, n, arr[30];
    printf("\nHow many number you want to sort : ");
    scanf("%d", &n);
    printf("Enter the numbers in to the array :\n");
    for (i = 0; i < n; ++i)
    {
        scanf("%d", &arr[i]);
    }
    printf("The elements of the array are : \n");
    for (i = 0; i < n; ++i)
    {
        printf("%d\t", arr[i]);
    }
    for (i = 0; i < n; ++i)
    {
        for (j = i + 1; j < n; ++j)
        {
            if (arr[i] > arr[j])
            {
                a =  arr[i];
                arr[i] = arr[j];
                arr[j] = a;
            }
        }
    }
    printf("\nThe elements of the array after sorting :\n");
    for (i = 0; i < n; ++i)
    {
        printf("%d\t", arr[i]);
    }
    printf("\n\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
}

Output:

= = = = = Sort the Array elements in ascending order = = = = =

How many number you want to sort : 10
Enter the numbers in to the array :
5
3
6
4
8
1
10
2
9
7
The elements of the array are :
5    3    6    4    8    1    10    2    9    7   
The elements of the array after sorting :
1    2    3    4    5    6    7    8    9    10   

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

No comments:

Post a Comment