There can be instances where the algorithm will have two paths to choose . Then what is the solution for that
Confusion in choosing next step
@coe17b030
I don’t understand which instance you are talking about. At every step we compare our current value with the key value to be searched. There can only be three possibilities.
- The key value matches our current value. You have found the desired value in our array.
if( key == a[i][j] )
// Success - Our current value is greater than the key value. Move downwards in this case
else if ( key < a[i][j] )
i = i + 1 ; // Move downwards - Our current value is less than the key value. Move left in this case
else if ( key > a[i][j] )
j = j - 1; // Move left
Suppose we are somewhere in the middle of the matrix and we have moved downward in the previous step and our desired value is in the right of our position but we can only move either left or downwards
@coe17b030
Staircase search is applied only which the array is sorted both rowwise and column wise . Hence such a case will never happen.
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.