Binary Search and Linear Search In Java | Code

  Linear Search and Binary Search In Java | Code

Linear Search:

Concept:

It involves checking each element in the array sequentially, one by one, from the beginning to the end.

It's analogous to looking for a specific book in a library by scanning each shelf from left to right, book by book.

Efficiency:

Time complexity is O(n), meaning the time it takes grows linearly with the size of the array.

It might need to examine every element in the worst case.


Example:



public class Lsearch {

    public static int linearSearch(int arr[], int key) {


        for (int i = 0; i < arr.length; i++) {

            if (arr[i] == key) {


                return 1;

            }


        }

        return -1;


    }


    public static void main(String[] args) {

        int arr[] = { 12, 21, 23, 45, 56, 67 };

        int mess = linearSearch(arr, 12);

        if (mess == 1) {

            System.out.println("value availble");

        } else {

            System.out.println("not availble");

        }

    }

}


Binary Search:

Concept:

It works by repeatedly dividing the search interval in half, eliminating half of the remaining elements at each step.

It's like guessing a number between 1 and 100 by repeatedly asking if it's higher or lower than a specific number, quickly narrowing down the possibilities.

Requirements:

The array must be sorted in ascending or descending order.

Efficiency:

Time complexity is O(log n), meaning the time it takes grows logarithmically with the size of the array.

It's significantly faster than linear search for large datasets.

Example : 


public class Bsearch {


    public static int bfun(int arr[], int key) {

        int s = 0;

        int e = arr.length;


        while (s <= e) {

            int mid = (s + e) / 2;

            if (arr[mid] == key) { // commparision

                return mid;

            }

            if (arr[mid] < key) {

                s = mid + 1;

            } else {

                e = mid - 1;

            }

        }

        return -1;

    }


    public static void main(String[] arg) {

        int arr[] = { 2, 3, 4, 5, 7, 6 };

        int k = 6;


        System.out.println("index no of key : " + bfun(arr,k));


    }

}


In vs Code

package array;

public class Bsearch {

    public static int bfun(int arr[], int key) {
        int s = 0;
        int e = arr.length;

        while (s <= e) {
            int mid = (s + e) / 2;
            if (arr[mid] == key) { // commparision
                return mid;
            }
            if (arr[mid] < key) {
                s = mid + 1;
            } else {
                e = mid - 1;
            }
        }
        return -1;
    }

    public static void main(String[] arg) {
        int arr[] = { 2, 3, 4, 5, 7, 6 };
        int k = 6;

        System.out.println("index no of key : " + bfun(arr,k));

    }
}



package array;

public class Lsearch {
    public static int linearSearch(int arr[], int key) {

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == key) {

                return 1;
            }

        }
        return -1;

    }

    public static void main(String[] args) {
        int arr[] = { 12, 21, 23, 45, 56, 67 };
        int mess = linearSearch(arr, 12);
        if (mess == 1) {
            System.out.println("value availble");
        } else {
            System.out.println("not availble");
        }
    }
}

No comments: