Write a C++ program to sort the array using bubble sort method.
Program Code:
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
Post A Comment:
0 comments: