Bubble Sort is one of the most basic type of sorting algorithm.Today I am going to tell you
how to code it.
PROGRAM
#include<iostream>
using namespace std;
int main()
{
int a[7]; // array declaration
for(int x=0;x<=6;x++) //loop for taking input
{
cout<<"enter the number:";
cin>>a[x]; // input given by user
}
// bubble sort starts
int i, j, temp;
for (i = 0; i <=6; ++i)
{
for (j = 0; j <= 6 - i; ++j )
{
if (a[j] > a[j+1]) // swap condition
{
// swapping
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
// loop to display outputs
for(j=0;j<=7;j++)
cout<<a[j];
return 0;
}
OUTPUT:
Comments
Post a Comment