if it is -
bool compare(int a, int b)
{ return a>b ;}
and
sort(a, a+n, compare)
does it mean if a > a+1 then swap?
Regarding the compare function
Hi @aslesha.j7
In comparator function, array send two of its value, and get return 1 if values need to be swapped else 0 if no swap. So a>b return 1 if a is greater than b meaning we need to swap the values. Hence sorting in ascending order.
Hope it Helps.
but a>b sorts in descending order
Hi @aslesha.j7
Yeah , sorry it sorts in descending order. Actually in practice we always do it in ascending order using b>a. So i confused it with that.
Hope it Helps.
it means if a[0]>a[1] it returns true,i.e, swap them, then it will be in ascending order but we are taught it is desc order. Im not able to understand how compare within sort function is working
Basic definition of comparator function is that it returns true if first variable should preceed the seccond variable. So we can apply it as: comp(int a,int b) - If we return a>b and it returns true meaning a should preceed b in the array. Hence it sorts in descending order. And when we write b>a and it returns true meaning a should preceed b meaning we sort in ascending order. Hope it is clear now.
okay thank you I got it