does it work for unsotred array?

function findMe (target, start, end){
      
      if(start > end) 
        return "Not Found" ;
      
      const middle = Math.floor( (start + end) / 2);
      
      if( arr[middle] === target ) 
        return `found at index ${middle}`;
        
      if( arr[middle] > target )
        return findMe( target, start, middle-1 );
        
      if( arr[middle] < target )
        return findMe( target, middle+1, end);
        
    }

since it's checking if
arr[middle]
is less than or bigger than the target if u given unsorted array for example
const arr = [3,1,2]

and we r looking for 3 then
it'll first land on 1, then check if 1 is smaller or bigger than 3. Since it's smaller than 3 it'll look on the right side but 3 in on the left side.
so does that mean it'll only work with sorted array or am I getting something wrong??
i do know how to sort an array btw xD
Was this page helpful?