Don't miss

## Bubble Sort

on 22:19

Bubble Sorting is the sorting algorithm or a type of sorting which is used to sort individual data items in ascending or descending order.Best case Complexity of selection sort is O(n).This sorting technique are also called as sinking sort.This technique is used in c ++,java,c# language etc.

Bubble Sorting Example and Algorithm:-

=5  7  4  3  2  6  1(Before Sort)
=5  4  7  3  2  6  1(Swap 7 & 3)
=5  4  3  7  2  6  1(Swap 7 & 2)
=5  4  3  2  7  6  1(Swap 7 & 6)
=5  4  3  2  6  7  1(Swap 7 & 1)
=5  4  3  2  6  1  7(Swap 5 & 4)
=4  5  3  2  6  1  7(Swap 5 & 3)
=4  3  5  2  6  1  7(Swap 5 & 2)
=4  3  2  5  6  1  7(Swap 6 & 1)
=4  3  2  5  1  6  7(Swap 4 & 3)
=3  4  2  5  1  6  7(Swap 4 & 2)
=3  2  4  5  1  6  7(Swap 5 & 1)
=3  2  4  1  5  6  7(Swap 3 & 2)
=2  3  4  1  5  6  7(Swap 4 & 1)
=3  2  1  4  5  6  7(Swap 3 & 1)
=2  1  3  4  5  5  7(Swap 2 & 1)

=1  2  3  4  5  6  7(After Sort)

Example of Bubble Sort(In Program):-

#include<iostream.h>
#include<conio.h>
void main()
{
int a[30],i,j,t,n;
cout<<"Enter size";
cin>>n;
cout<<"Enter array element";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}

}
cout<<"Array as";

for(i=0;i,n;i++)
cout<<a[i];
getch();
}

That's all about Bubble Sorting in data structure and computer science.