Bubble Sort Technique

Bubble Sort Technique

Program to implement the Bubble Sort Technique


In this technique, starting from the first element to the last element of the list, every 2 consecutive elements r compared and arranged in order. This will take 'n-1' iterations if there are 'n' elements to be sorted.




Bubble Sort Program 

#include<iostream.h>
#include<conio.h>
void main()
{
int List[100],n,i,j;
cout<<"Enter the size of the list\n";
cin>>n;
cout<<"Enter the elements of the list\n";
for(i=0;i<n;i++)
cin>>List[i];
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(List[j]>List[j+1])
{
int t = List[j];
List[j]=List[j+1];
List[j+1]=t;
}
}
}
cout<<"The Elments in Ascending Order after Bubble Sort are:\n";
for(i=0;i<n;i++)
cout<<List[i]<<", ";
getch();
}

Comments