Sorting and Binary search using Java

前端 未结 4 671
离开以前
离开以前 2020-12-18 17:17

I was asked to sort and search an array. The sorting the array was simple and my code worked but then whenever I try to call the binary search method it works for the first

4条回答
  •  -上瘾入骨i
    2020-12-18 17:28

    You did a mistake in calling the rBsearch method in the following lines Instead of

    else if (L[mid] < k) {
            return rBsearch(L, k, mid + 1, high); 
        } else {
            return rBsearch(L, k, low, mid - 1);
        }
    

    You should use

    else if (L[mid] < k) {
                return rBsearch(L,  mid + 1, high,k); //the order of the parameters
            } else {
                return rBsearch(L, low, mid - 1,k);
            }
    

提交回复
热议问题