Searching

less than 1 minute read

Searching

Liner Search :

Binary Search : O(log n). n,n/2,n/4,n/8,….

Pre processing over head. Elements need to be in sorted order.

Pick the middle element and match if the element is the requested element, else check if the mid is > or < than the requested element. based on that recursively call the binary search.

public boolean binarySearch(int[] arr, int x){

    int mid = arr.length/2;

    if (arr[mid] == x)
        return true;
    else if(arr[mid] > x)
        binarySearch(arr,x)
    else if(arr[mid] < x)
        binarySearch()
}

int mid = l + (h-l)/2