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
parent
ef3cd5063f
commit
ada02c2b40
|
@ -13,8 +13,10 @@ int main()
|
||||||
{ //array elements input
|
{ //array elements input
|
||||||
cin>>arr[i];
|
cin>>arr[i];
|
||||||
}
|
}
|
||||||
|
bool swap;
|
||||||
for (int i = 0; i < n; i++)
|
for (int i = 0; i < n; i++)
|
||||||
{
|
{
|
||||||
|
swap = false;
|
||||||
for (int j= 0; j< n-1; j++)
|
for (int j= 0; j< n-1; j++)
|
||||||
{ //comparing adjecent elements of array
|
{ //comparing adjecent elements of array
|
||||||
if (arr[j]>arr[j+1])
|
if (arr[j]>arr[j+1])
|
||||||
|
@ -22,8 +24,13 @@ int main()
|
||||||
temp = arr[j];
|
temp = arr[j];
|
||||||
arr[j] = arr[j+1];
|
arr[j] = arr[j+1];
|
||||||
arr[j+1] = temp;
|
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)
|
for (int i = 0; i < n; ++i)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue