Search This Blog

Friday, 21 August 2020

Bubble sort

 #include<iostream>

#include<conio.h>

using namespace std;

int main()

{

int a[20],size,i,j,temp,swap;

cout<<"\n\n\tEnter the size of array : ";

cin>>size;

cout<<"\n\tEntwr the array elements :";

for(i=0;i<size;i++)

{

cin>>a[i];

}

for(i=0;i<(size-1);i++)

{

swap=0;

for(j=0;j<(size-1)-1;j++)

{

if(a[j]<a[j+1])

{

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

swap=1;

}

}

if(swap==0)

{

break;

}

}

cout<<"\n\n\tArray after bubble sort :";

for(i=0;i<size;i++)

{

cout<<" "<<a[i];

}

getch();

return 0;

}


No comments:

Post a Comment

Search This Blog

Contact Form

Name

Email *

Message *

Popular Posts