Skip to main content

Array sorting in C

 


#include <stdio.h>
int main()
{
    int a[] = {5,4,3,2,1};
    int n;
    n = sizeof(a)/sizeof(int);
    // printf("Enter the length of the array: ");
    // scanf("%d",&n);
    // for (int i = 0; i <n; i++)
    // {
    //     scanf("%d",&a[i]);
    // }
    for (int i = 0; i < n; i++)
    {
        printf("%d",a[i]);
    }
    printf("\n");
    for (int i = 0; i <n; i++)
    {
        for (int j = i+1; j <n; j++)
        {
            if(a[j]<a[i]){
                int temp;
                temp = a[j];
                a[j] = a[i];
                a[i] = temp;
            }
        }
       
    }
    for (int i = 0; i < n; i++)
    {
        printf("%d",a[i]);
    }
   
   
   
    return 0;
}

Comments