C program to sort array in ascending order is to sort the given array in ascending order.

What is sorting?
It is technique to sort array or arrange data in some order either ascending or descending order.

Example :-
array [] = 30, 10, 20
Sorted list in ascending order is
array [] = 10, 20, 30

Algorithm to sort array in ascending order

  1. Declare variables. N, a[]
  2. Take array input from a user. a[N]
  3. Traverse array
    For i=0 to end
    If a[i] > a[i+1]
    swap a[i] and a[i+1]
    Else continue
  4. If a[i]== end
    stop
  5. Print sorted array

C program to sort array in ascending order

#include<stdio.h>
main()
{
int i,j,num,temp;
printf("Enter number of elements\n");
scanf("%d",&num);
int a[num];
printf("Enter numbers\n");
for(i=0;i<num;i++)
{
scanf("%d",&a[i]);
}

for(i=0;i<num;i++)
{
for(j=i+1;j<num;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Ascending oreder of given numbers is\n");
for(i=0;i<num;i++)
{
printf("%d\n",a[i]);
}
}

Output :

C program to sort array in ascending order

Write A Comment