If condition not understand

recursion in if conditon
how it will work?
if (arr[0] < arr[1] and isSorted(arr + 1, n - 1))

hello @amit0001

isSorted(a,n) function will tell whether array a with size n is sorted or not.

now how to break this problem in smaller subproblems?
the idea is to compare first two elements (arr[0],arr[1]) and call the same function on remaining array.

if the first two element are in correct order and remaining element are also in correct order then return true
otherwise return false

in syntax wise

if(arr[0] <= arr[1] && isSorted(arr+1,n-1) )
{
return true;
}else{
return false;
}

okay, thank you
I mark as resolve

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.

one more please, why we take array as pointer “bool isSorted(int *arr, int n)”

bool isSorted(int *arr,int n)
is same as
bool isSorted(int arr[],int n)
u can use any of them

in both the cases base address of array is passed.

1 Like