1
\$\begingroup\$

I have written a binary search algorithm, but it seems to be a bit different than other peoples that I've seen. I was hoping that the community could give me some feedback as to whether or not I'm missing something, or doing something the wrong way.

Binary Search:

import java.util.Arrays; 

public class BinarySearch {

    public int[] numbers = new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; 

    /**
     * @param num
     * @param key
     * 
     * Function recursively searches sorted array of integers, finding the specific number (key).
     * Search looks at the midpoint of array, checking to see if midpoint is number being sought,
     * if not, depending of whether the sought number is greater than, or less than, the midpoint
     * the function copies the upper, or lower, half of the array and passes it into a recursive 
     * function call.
     * 
     */
    public int performSearch(int[] num, int key){
        if(num.length == 0){
            System.out.println("Array empty"); 
            return 0; 
        }else{
            int mid; 
            int number=0; 
            mid = (num.length)/2; 
            if(key == num[mid]){
                number =  num[mid]; 
                System.out.println("Found the number " + number); 
                return number; 
            }else if((key < num[mid]) && num.length > 1){
                num = Arrays.copyOfRange(num, 0, mid); 
                System.out.println("Low Range: " + Arrays.toString(num)); 
                return performSearch(num, key); 
            }else if((key > num[mid]) && num.length > 1){
                num = Arrays.copyOfRange(num, mid, num.length); 
                System.out.println("High Range: " + Arrays.toString(num)); 
                return performSearch(num, key); 
            }else{
                System.out.println("Number does not exist in array."); 
                return 0; 
            }
            //return number; 
        }

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        int key = 22; 
        BinarySearch bs = new BinarySearch(); 
        int index = bs.performSearch(bs.numbers, key); 
        System.out.println("Number " + index);
    }

}
\$\endgroup\$
1
  • 2
    \$\begingroup\$ one problem I see with the code is that you are returning "num[mid]" where you should be returning "mid" in case the number is found! \$\endgroup\$ May 5, 2013 at 3:31

1 Answer 1

4
\$\begingroup\$

I see 1 bug, 1 problem and 1 minor thing:

  • as listed in the comment above: You are returning the number instead of the index. That's not very helpful for a binary search algorithm ;)
  • when the array does not contain the item you are returning 0. 0 is a valid array index, by convention you'd return -1 in that case.
  • not much of a problem but unnecessary: local variable number in first else block is obsolete.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.