Bubble Sorting, Sorting in C++, Bubble sorting in C++
Write a C++ program to sort the array using bubble sort method.

Program Code:
#include<iostream>
using namespace std;
int main()
{
int n, i, j, temp;
int array[50];
cout<<"Enter total number of elements in the array :";

cin>>n;
cout<<endl;
cout<<"Enter element in the array :";
cout<<endl;
for(i=0; i<n; i++)
{
cin>>array[i];
}
// Sorting array using bubble sort technique
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
cout<<"Sorted list is : ";
cout<<endl;
for(i=0; i<n; i++)
{
cout<<array[i]<<" ";
}
return 0;

}


The program output is tested on www.jdoodle.com
Output:
Enter total number of elements in the array :

Enter element in the array : 9
2 4 5 1 3 7 8 9 0
Sorted list is :
0 1 2 3 4 5 7 8 9


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: