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

40 lines
499 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;
cin >> n;
int Array[n];
2016-07-16 18:56:48 +08:00
cout<<"\nEnter any 6 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
{
for(int j=0; j<n-1; j++)
2016-07-16 18:56:48 +08:00
{
if(Array[j]>Array[j+1])
{
int temp=Array[j];
Array[j]=Array[j+1];
Array[j+1]=temp;
}
}
}
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";
}
}