Java BinarySearch

前端 未结 10 2120
忘了有多久
忘了有多久 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:55

    Well I know I am posting this answer much later.
    But according to me its always better to check boundary condition at first.
    That will make your algorithm more efficient.

        public static int binarySearch(int[] array, int element){
        if(array == null || array.length == 0){  // validate array
            return -1;
        }else if(element<array[0] || element > array[array.length-1]){ // validate value our of range that to be search 
            return -1;
        }else if(element == array[0]){  // if element present at very first element of array
            return 0;
        }else if(element == array[array.length-1]){ // if element present at very last element of array
            return array.length-1;
        }
    
        int start = 0;
        int end = array.length-1;
    
        while (start<=end){
            int midIndex = start + ((end-start)/2);   // calculate midIndex
            if(element < array[midIndex]){  // focus on left side of midIndex
                end = midIndex-1;
            }else if(element > array[midIndex]){// focus on right side of midIndex
                start = midIndex+1;
            }else {
                return midIndex;   // You are in luck :)
            }
        }
        return -1; // better luck next time :(
    }
    
    0 讨论(0)
  • 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));
    
    0 讨论(0)
  • 2020-12-09 23:56
       /**
         * Find whether 67 is a prime no
         * Domain consists 25 of prime numbers
         * Binary Search
         */
    
        int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
    
        int min = 0,
                mid,
                max = primes.length,
                key = 67,
                count= 0;
    
        boolean isFound = false;
    
    
        while (!isFound) {
            if (count < 6) {
              mid = (min + max) / 2;
                if (primes[mid] == key) {
                    isFound = true;
                    System.out.println("Found prime at: " + mid);
                } else if (primes[mid] < key) {
                    min = mid + 1; 
                    isFound = false;
                } else if (primes[mid] > key) { 
                    max = mid - 1; 
                    isFound = false;
                }
                count++;
    
            } else {
                System.out.println("No such number");
                isFound = true;
            }
    
        }
    
    0 讨论(0)
  • 2020-12-09 23:57

    I somehow find the iterative version not quite easy to read, recursion makes it nice and easy :-)

    public class BinarySearch {
    
        private static int binarySearchMain(int key, int[] arr, int start, int end) {
            int middle = (end-start+1)/2 + start; //get index of the middle element of a particular array portion
    
            if (arr[middle] == key) {
                return middle;
            }
    
            if (key < arr[middle] && middle > 0) {
                return binarySearchMain(key, arr, start, middle-1); //recurse lower half
            }
    
            if (key > arr[middle] && middle < arr.length-1) {
                return binarySearchMain(key, arr, middle+1, end); //recurse higher half
            }
    
            return Integer.MAX_VALUE; 
        }
    
        public static int binarySearch(int key, int[] arr) { //entry point here
            return binarySearchMain(key, arr, 0, arr.length-1);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题