enh(CPlusPlus): bubble-sort (#302)

* Add binary-search-tree

* Optimize the bubble sort

* Revert "Add binary-search-tree"

This reverts commit 8d712e7dcb.
pull/308/head
Rahul Rajeev Pillai 2021-05-15 23:44:44 +05:30 committed by GitHub
parent ef3cd5063f
commit ada02c2b40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 0 deletions

View File

@ -13,8 +13,10 @@ int main()
{ //array elements input
cin>>arr[i];
}
bool swap;
for (int i = 0; i < n; i++)
{
swap = false;
for (int j= 0; j< n-1; j++)
{ //comparing adjecent elements of array
if (arr[j]>arr[j+1])
@ -22,8 +24,13 @@ int main()
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
swap = true;
}
}
// break if no swap takes place in inner loop
// it means the array is sorted
if(!swap){ break; }
}
for (int i = 0; i < n; ++i)
{