In the compare function
When we write return a < b the output is in decreasing order and vice - versa.
Please explain why it is so ? Because generally when we pass compare functn with return a > b it sorts in decreasing order.
In the compare function
When we write return a < b the output is in decreasing order and vice - versa.
Please explain why it is so ? Because generally when we pass compare functn with return a > b it sorts in decreasing order.
No, I am talking about the comparator function which is being passed as a parameter in bubble sort function.
In bubble sort comparator is defined as
cmp(int a,int b){
return a> b;
}
it means when first one (a) is bigger than second one (b) then return true
otherwise return false.
now in algorithm
bhaiya has written this if statemet
if(compare(a[i],a[i+1]){
swap(a[i],a[i+1]);
}
this if statement will execute only when compare function will return i,e when a[i] > a[i+1].
and after execution of this if satement a[i] a[i+1] will change there values ie now a[i] < a[i+1]
and that is why it is sorting elements in ascending order
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.