To write a C program to input N integers into a single dimension array and sort them in ascending order suing bubble sort technique.
Here's a diagramatic explanation :

C code :
#include<stdio.h>
main()
{
int i,j,n,a[100],t;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements of the array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("The unsorted array is :\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
for(i=0;i<n-1;i++) //Actual bubble sort begins
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
printf("The sorted array is :\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
Here's a diagramatic explanation :
C code :
#include<stdio.h>
main()
{
int i,j,n,a[100],t;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements of the array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("The unsorted array is :\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
for(i=0;i<n-1;i++) //Actual bubble sort begins
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
printf("The sorted array is :\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
No comments:
Post a Comment