Q1: Write a C program for BubbleSort ?
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,n,tmp,a[20];
clrscr();
//Intialising Array
printf("\nEnter the number of elements--\t");
scanf("%d",&n);
printf("\nThe Elements are----\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);}
//Bubble Sort Algorithm
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
//Ascending Order
if(a[j]>a[j+1]){
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
}
//Output
printf("After BubbleSort\n");
for(j=0;j<n;j++)
{
printf("%d\t",a[j]);
}
getch();
}
***********************************************************************************
Sample Output:
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,n,tmp,a[20];
clrscr();
//Intialising Array
printf("\nEnter the number of elements--\t");
scanf("%d",&n);
printf("\nThe Elements are----\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);}
//Bubble Sort Algorithm
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
//Ascending Order
if(a[j]>a[j+1]){
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
}
//Output
printf("After BubbleSort\n");
for(j=0;j<n;j++)
{
printf("%d\t",a[j]);
}
getch();
}
***********************************************************************************
Sample Output:
Comments
Post a Comment
Thanks for the comment and don't forget to subscribe.