TheAlgorithms-C-Plus-Plus/Sorting/Bubble Sort.cpp

47 lines
579 B
C++
Raw Normal View History

2016-07-16 18:56:48 +08:00
//Bubble Sort
#include<iostream>
using namespace std;
int main()
{
int n;
short swap=0;
cin >> n;
int Array[n];
cout<<"\nEnter any "<<n<<" Numbers for Unsorted Array : ";
2016-07-16 18:56:48 +08:00
//Input
for(int i=0; i<n; i++)
2016-07-16 18:56:48 +08:00
{
cin>>Array[i];
}
2016-07-16 18:56:48 +08:00
//Bubble Sorting
for(int i=0; i<n; i++)
2016-07-16 18:56:48 +08:00
{
swap=0;
for(int j=0; j<n-1-i; j++)
2016-07-16 18:56:48 +08:00
{
if(Array[j]>Array[j+1])
{
swap=1;
2016-07-16 18:56:48 +08:00
int temp=Array[j];
Array[j]=Array[j+1];
Array[j+1]=temp;
}
}
if(swap == 0)
{
break;
}
2016-07-16 18:56:48 +08:00
}
2016-07-16 18:56:48 +08:00
//Output
cout<<"\nSorted Array : ";
for(int i=0; i<n; i++)
2016-07-16 18:56:48 +08:00
{
cout<<Array[i]<<"\t";
}
}