Home > front end >  binary search on array giving wrong index on java
binary search on array giving wrong index on java

Time:12-10

i'm writing a code that does not have to import java Arrays, and that inserts, deletes, and searches a series of values. but the output prints out that it does not find the values on the list when the binSearch() method is called. i have already sorted the array in the main method before calling the binSearch() but it still doesnt find the index of the values.

i'm still a beginner in java, so it would be nice of you if you can explain it to me at a beginner level.

    private static int[] insertElement(int index, int array[], int list[], int i) {
        int length = array.length;
        int destination[] = new int[length   1];
        for (int j = 0; j < destination.length; j  ) {
            System.arraycopy(array, 0, destination, 0, index);
            System.arraycopy(array, index, destination, index   1, length - index);
        }
        destination[index] = list[i];
        System.out.println(list[i]   " is inserted in the list.");
        return destination;
    }

    private static int[] deleteElement(int index, int[] array, int list[], int i) {
        boolean[] deleteNumber = new boolean[array.length];
        int size = 0;
        for (int j = 0; j < array.length; j  ) {
            if (array[j] == list[i]) {
                deleteNumber[j] = true;
                System.out.println(list[i]   " is removed from the list.");
            }
            else {
                deleteNumber[j] = false;
                size  ;
            }
        }
        
        int[] newArr = new int[size];
        int in = 0;
        for (int j = 0; j < array.length; j  ) {
            if (!deleteNumber[j]) {
                newArr[in  ] = array[j];
            }
        }
        return newArr;
    }

    public static int binSearch(int[] array, int search[], int i) {
        int left = 0;
        int right = array.length - 1;
        
        if (left <= right) {
            int middle = (left   right) / 2;
            if (search[i] < array[middle]) {
                right = middle - 1;
            }
            else if (search[i] > array[middle]) {
                left = middle   1;
            }
            else {
                System.out.print(search[i]   " is found at location ");
                return middle;
            }
        }
        return -1;
    }

     public static void main(String args[]) {
        int arr[] = {2, 4, 6, 8, 9, 10, 20, 33, 44, 45, 68, 88};
        int index = 1;
        
        //insert element
        int newIndex = index - 1;
        int s[] = {3, 78, 98, 12};
        for (int i = 0; i < s.length; i  ) {
            arr = insertElement(newIndex, arr, s, i);
        }
        
        //delete element
        int d[] = {20, 44, 89};
        for (int i = 0; i < d.length; i  ) {
            arr = deleteElement(newIndex, arr, d, i);
        }
        
        //sort the array
        for (int i = 0; i < arr.length; i  ) {
            for (int j = i   1; j < arr.length; j  ) {
                if (arr[i] > arr[j]) {
                    int swap = arr[i];
                    arr[i] = arr[j];
                    arr[j] = swap;
                }
            }
        }
        
        //search for element in array
        BinarySearch ob = new BinarySearch();
        int a[] = {8, 45, 88, 90};
        for (int i = 0; i < a.length; i  ) {
            int result = ob.binSearch(arr, a, i);
            if (result == -1) {
                System.out.println(a[i]   " is not found in the list.");
            }
            else {
                System.out.println(result   ".");
            }
        }
        //print array
        printArray(arr);
    }

CodePudding user response:

i found the error in my code. the binSearch() method should have used a while loop, instead of the if loop. the whole code now perfectly runs.

    public static int binSearch(int[] array, int search[], int i) {
        int left = 0;
        int right = array.length - 1;
        
        while (left <= right) {
            int middle = (left   right) / 2;
            if (search[i] < array[middle]) {
                right = middle - 1;
            }
            else if (search[i] > array[middle]) {
                left = middle   1;
            }
            else {
                System.out.print(search[i]   " is found at location ");
                return middle;
            }
        }
        return -1;
    }

CodePudding user response:

Just replace the if clause with while as you have to search at all levels of the binary search tree. Change

if (left <= right)

to

while (left <= right)

And also there is small issue in your insertElement method.

for (int j = 0; j < destination.length; j  ) {
            System.arraycopy(array, 0, destination, 0, index);
            System.arraycopy(array, index, destination, index   1, length - index);
        }

There is no need of for loop again in the insertElement method as you are already looping through the array that needs to be added in your main method.

  • Related