Java BinarySearch

前端 未结 10 2160
忘了有多久
忘了有多久 2020-12-09 23:33

Can I get some help please? I have tried many methods to get this to work i got the array sorted and to print but after that my binary search function doesnt want to run and

10条回答
  •  不知归路
    2020-12-09 23:56

    Here

        if (mid > key) 
            high = mid -1;
        else if (mid < key) 
            low = mid +1;
        else 
            return mid;
    

    You're comparing index to a value (key) in array. You should instead compare it to a[mid]

    And,

    System.out.println("Found " + key + " at " + binarySearch(double a[], key));
    

    Should be

    System.out.println("Found " + key + " at " + binarySearch(a, key));
    

提交回复
热议问题