Bubble Sort Code in C++


 Other Type Of Sorting : Click here :: most important ::



#include<iostream.h>
#include<conio.h>

void display(int *arr,int n)
{
 cout<<endl;
 for(int i=0;i<=n;i++){
 cout<<arr[i]<<"\t";  }
}

void bubblesort(int *arr,int n)
{
 int i,temp,bound=n-1;
 int swapped=1;
 while(swapped>0)
 {
 swapped=0;
 for(i=0;i<=bound;i++){ display(arr,6);
 if(arr[i]>arr[i+1]){
  cout<<endl<<"Swaped arr["<<i<<"] with arr["<<i+1<<"]\n";
 temp=arr[i];
 arr[i]=arr[i+1];
 arr[i+1]=temp;
 swapped=i;          }}
 bound=swapped;
 }
}

void main()
{
int x[]={12,7,5,4,11,9,8};
clrscr();
bubblesort(x,6);
display(x,6);
getch();
}

No comments

Powered by Blogger.