/* Write a program in C to sort elements of array in ascending order. */
#include <stdio.h>
#include <stdio.h>
int main()
{
int a[50];
int n, i, j, tmp;
printf("Input the size of array : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(a[i] > a[j])
{
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
printf("\nSorted ascending order:\n");
for(i=0; i<n; i++)
printf("%d ", a[i]);
}
{
int a[50];
int n, i, j, tmp;
printf("Input the size of array : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&a[i]);
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(a[i] > a[j])
{
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
printf("\nSorted ascending order:\n");
for(i=0; i<n; i++)
printf("%d ", a[i]);
}
Output:
Input the size of array : 5
element - 0 : 45
element - 1 : 12
element - 2 : 78
element - 3 : -33
element - 4 : 59
Sorted ascending order:
-33 12 45 59 78
0 Comments