Que: Write a program to sort a one-dimensional array in ascending order
Code:
#include <stdio.h>
int main()
{
int arr[100],n,i,j;
int temp;
printf("Enter total number of elements: ");
scanf("%d",&n);
//read array elements
printf("Enter array elements:\n");
for(i=0;i< n;i++)
{
printf("Enter element %d: ",i+1);
scanf("%d",&arr[i]);
}
//find ascending
for(i=0;i< n;i++)
{
for(j=i+1;j< n;j++)
{
if(arr[i]>arr[j])
{
temp =arr[i];
arr[i] =arr[j];
arr[j] =temp;
}
}
}
printf("\nArray elements after sorting:\n");
for(i=0;i< n;i++)
{
printf("%d\n",arr[i]);
}
return 0;
}
Output:
Code:
#include <stdio.h>
int main()
{
int arr[100],n,i,j;
int temp;
printf("Enter total number of elements: ");
scanf("%d",&n);
//read array elements
printf("Enter array elements:\n");
for(i=0;i< n;i++)
{
printf("Enter element %d: ",i+1);
scanf("%d",&arr[i]);
}
//find ascending
for(i=0;i< n;i++)
{
for(j=i+1;j< n;j++)
{
if(arr[i]>arr[j])
{
temp =arr[i];
arr[i] =arr[j];
arr[j] =temp;
}
}
}
printf("\nArray elements after sorting:\n");
for(i=0;i< n;i++)
{
printf("%d\n",arr[i]);
}
return 0;
}
Output:
No comments:
Post a Comment