Description:
Bubble
sort is the simplest and oldest sorting technique. This method takes two
elements at a time. It compare these two elements. If first elements is less
than second one, they are left undistrurbed. If the first element is greater
then second one then they are swapped. The procedure continues with the next
two elements goes and ends when all the elements are sorted.But bubble sort is an
inefficient
algorithm. The order of bubble
sort algorithm is O(n2).
Program:
#include<stdio.h>
#include< conio.h>
void main()
{
int i,j,t,a[5],n;
clrscr();
printf("enter the range of array:"); scanf("%d",&n);
printf("enter elements into array:"); for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
printf("the sorted order is:"); for(i=0;i<n;i++) printf("\t%d",a[i]);
getch();
}
Input/Output:
enter the range of array:3
enter elements into array:3 2 1
the sorted order is:1 2 3
enter the range of array:5
enter elements into array:56 23 34 12 8
the sorted order is: 8 12 23 34 56
VIVA QUESTIONS
1) Define bubble sort ?
Ans: : Bubble sort is the simplest and oldest sorting technique. This method takes two elements at a time. It compare these two elements. If first elements is less than second one, they are left undistrurbed. If the first element is greater then second one then they are swapped. The procedure continues with the next two elements goes and ends when all the elements are sorted.
2) display the efficiency of bubble sort ?
Ans : O(n2)
#include<stdio.h>
#include< conio.h>
void main()
{
int i,j,t,a[5],n;
clrscr();
printf("enter the range of array:"); scanf("%d",&n);
printf("enter elements into array:"); for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
printf("the sorted order is:"); for(i=0;i<n;i++) printf("\t%d",a[i]);
getch();
}
Input/Output:
enter the range of array:3
enter elements into array:3 2 1
the sorted order is:1 2 3
enter the range of array:5
enter elements into array:56 23 34 12 8
the sorted order is: 8 12 23 34 56
VIVA QUESTIONS
1) Define bubble sort ?
Ans: : Bubble sort is the simplest and oldest sorting technique. This method takes two elements at a time. It compare these two elements. If first elements is less than second one, they are left undistrurbed. If the first element is greater then second one then they are swapped. The procedure continues with the next two elements goes and ends when all the elements are sorted.
2) display the efficiency of bubble sort ?
Ans : O(n2)
I like your blog, thank you for posting.
ReplyDelete