Skip to main content

C #7-Sorting of Elements in an Array

Source Code:

#include <stdio.h>
void main()
{
    int arr[30];
    int i, j, num, temp;
    printf("Enter the number of values to be taken in array \n");
    scanf("%d", &num);
    printf("Enter the elements to array\n");

    for (i = 0; i < num; i++)
    {
        scanf("%d", &arr[i]);
    }

//Sorting Operation Starts

    for (i = 0; i < num; i++)
    {
        for (j = 0; j < (num - i - 1); j++)
        {
            if (arr[j] > arr[j + 1])
            {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
    printf("After Sorting :\n");
    for (i = 0; i < num; i++)
    {
        printf("%d\n", arr[i]);
    }
}

Output:

D:\D INSTALL\TDM-GCC-32\Programs>gcc Sorting.c -o Sort.exe

D:\D INSTALL\TDM-GCC-32\Programs>sort.exe
Enter the number of values to be taken in array
5
Enter the elements to array
5
6
3
2
1
After Sorting :
1
2
3
5
6


Also See : 

  • Addition two number using C Program
  • Palindrome string or not using C Program


Thanks and Regards,
Tech Bird

Comments