From c614c91f8a70ba47f22c961772585e88c99673df Mon Sep 17 00:00:00 2001 From: UTpH Date: Tue, 16 Oct 2018 00:54:33 -0400 Subject: [PATCH] Modified C++ Bubble sort (#19358) Optimized by stopping the algorithm if there wasn't any swap(rest is sorted). There was a syntax error(temp was not declared) --- .../algorithms/sorting-algorithms/bubble-sort/index.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/client/src/pages/guide/english/algorithms/sorting-algorithms/bubble-sort/index.md b/client/src/pages/guide/english/algorithms/sorting-algorithms/bubble-sort/index.md index 55325a6764..05d49f218c 100644 --- a/client/src/pages/guide/english/algorithms/sorting-algorithms/bubble-sort/index.md +++ b/client/src/pages/guide/english/algorithms/sorting-algorithms/bubble-sort/index.md @@ -114,17 +114,20 @@ void bubblesort(int arr[], int n) { if(n==1) //Initial Case return; - + bool swap_flag = false; for(int i=0;iarr[i+1]) { - temp=arr[i]; + int temp=arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; + swap_flag = true; } } - + // IF no two elements were swapped in the loop, then return, as array is sorted + if(swap_flag == false) + return; bubblesort(arr,n-1); //Recursion for remaining array } ```