Binary search of a sorted array

前端 未结 6 973
南方客
南方客 2021-02-04 10:04

I am trying to search a descending sorted array using this binary search code. However, after I sort it, and try to search, it doesn\'t come back with any result, just a loading

6条回答
  •  悲&欢浪女
    2021-02-04 10:58

    Shot in the dark:

    if (target < mynumbers[mid]) 
       first = mid + 1; 
    else if (target > mynumbers[mid]) 
       last = mid - 1; 
    else 
    {
        ....
        break;
    }
    

    I suspect you're bouncing back and forth between mid+1 and mid-1

提交回复
热议问题