Merge pull request #7 from Roshaen/master

Added bubble sort algorithm in C++
pull/8/head
Ananthakrishnan Nair RS 2021-01-13 21:12:03 +05:30 committed by GitHub
commit 0df8cfed1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,33 @@
//bubble sort
# include <iostream>
using namespace std;
int main()
{
cout<<"Enter the length of array"<<endl;
int m, temp,n;
cin>>n;
int arr[n];
cout<<"Enter the elements of array"<<endl;
for (int i = 0; i < n; i++)
{ //array elements input
cin>>arr[i];
}
for (int i = 0; i < n; i++)
{
for (int j= 0; j< n-1; j++)
{ //comparing adjecent elements of array
if (arr[j]>arr[j+1])
{ // swaping array elements
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for (int i = 0; i < n; ++i)
{
cout<<arr[i]<<" ";
}
return 0;
}