Code: Sort in an ascending Order
#include<stdio.h>
#include<conio.h>
int j;
void main()
{
int a[100];
int n,i;
printf("enter the array limit\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter array elements a[%d]\t",i);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
int j;
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
int temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("sorted array is:\n");
for(j=1;j<=n;j++)
{
printf("%d",a[j]);
}
getch();
}
Output:
Code: Sort in an descending Order
#include<stdio.h>
#include<conio.h>
int j;
void main()
{
int a[100];
int n,i;
printf("enter the array limit\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter array elements a[%d]\t",i);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
int j;
for(j=0;j<n-i;j++)
{
if(a[j]<a[j+1])
{
int temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("sorted array is:\n");
for(j=0;j<n;j++)
{
printf("%d",a[j]);
}
getch();
}
Output:
More Tutorials
C program to bubble sort an array in ascending and descending order
#include<stdio.h>
#include<conio.h>
int j;
void main()
{
int a[100];
int n,i;
printf("enter the array limit\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter array elements a[%d]\t",i);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
int j;
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
int temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("sorted array is:\n");
for(j=1;j<=n;j++)
{
printf("%d",a[j]);
}
getch();
}
Output:
Code: Sort in an descending Order
#include<stdio.h>
#include<conio.h>
int j;
void main()
{
int a[100];
int n,i;
printf("enter the array limit\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter array elements a[%d]\t",i);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
int j;
for(j=0;j<n-i;j++)
{
if(a[j]<a[j+1])
{
int temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("sorted array is:\n");
for(j=0;j<n;j++)
{
printf("%d",a[j]);
}
getch();
}
Output:
More Tutorials