Write a program in C to sort elements of array in ascending order

 

#include<stdio.h>

int main()

{

int n, i, j, tmp;

printf(“Enter the size of array: “);

scanf(“%d”,&n);

int arr[n], shorted[n];

for(i=0; i<n; i++){

printf(”  Enter the %dth element:: “,i+1);

scanf(“%d”, &arr[i]);

}

system(“cls”);

printf(“n  Entered array::”);

for(i=0; i<n; i++){

printf(”  %d  “,arr[i]);

}

for(i=0; i<n; i++){

for(j=i+1; j<n; j++){

if(arr[j]<arr[i]){

tmp=arr[j];

arr[j]=arr[i];

arr[i]=tmp;

}

}

}

printf(“n  Elements of array in sorted ascending order:n”);

    for(i=0; i<n; i++)

    {

        printf(”  %d  “, arr[i]);

    }

return 0;

}

Leave a Reply

You cannot copy content of this page