Search
 
SCRIPT & CODE EXAMPLE
 

CPP

recursive binary search

int binarySearch(int[] A, int low, int high, int x)
{
    if (low > high) {
        return -1;
    }
    int mid = (low + high) / 2;
    if (x == A[mid]) {
        return mid;
    }
    else if (x < A[mid]) {
        return binarySearch(A, low,  mid - 1, x);
    }
    else {
        return binarySearch(A, mid + 1,  high, x);
    }
}
Comment

binary search recursive

"""Binary Search
Recursive
	- 2 Separate functions
    	--> Perhaps more readable?
    - Or just one function that recursively calls itself
		--> Perhaps more logical?
"""

## With 2 separate functions
def bin_recur(lst, item):
    return go_bin_recur(lst, item, 0, len(lst) - 1)
    
def go_bin_recur(lst, item, low, high):
    if low <= high:
        mid = (low + high) // 2
        if item > lst[mid]: # To the left
            low = mid + 1 
        elif item < lst[mid]: # To the right
            high = mid - 1
        else: # Found
            return mid
        return go_bin_recur(lst, item, low, high)
    return [] # Not found


## With one function
def bin_recur(lst, item, low=0, high=None):
    if high is None:
        high = len(lst) - 1
    if low <= high:
        mid = (low + high) // 2
        if item > lst[mid]: # To the left
            low = mid + 1 
        elif item < lst[mid]: # To the right
            high = mid - 1
        else: # Found
            return mid
        return bin_recur(lst, item, low, high)
    return [] # Not found

"""Whats your preference?"""
Comment

Binary Search in C-Recursive Method

int bSearch(int arr[], int left, int right, int target) {
    if (right >= left) {
        int mid = left + (right - left) / 2;
 
        if (arr[mid] == target)
            return mid;
 
        if (arr[mid] > target)
            return binarySearch(arr, left, mid - 1, target);
 
        return binarySearch(arr, mid + 1, right, target);
    }
    return -1;
}
Comment

C++, binary search recursive

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int ret = 0;
		
        if (nums.size() == 0)
            return -1;
  
        if (nums[nums.size()/2] < target) {
            vector<int> halfVec(nums.begin()+nums.size()/2+1,nums.end());
            auto retIdx = search(halfVec,target);
            if (retIdx == -1) return -1;
            ret += retIdx +  nums.size()/2+1;
        } else if (nums[nums.size()/2] > target) {
            vector<int> halfVec(nums.begin(),nums.begin()+nums.size()/2);
            ret = search(halfVec,target);
        } else {
            ret = nums.size()/2;
        }
         
        return ret;
        
    }
};
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ hash 
Cpp :: Maximum sum of non consecutive elements 
Cpp :: find factorial in c++ using class 
Cpp :: dangling pointer 
Cpp :: options select from array 
Cpp :: how we can write code to remove a character in c++ 
Cpp :: c++ length of int 
Cpp :: how to take input in 2d vector in c++ 
Cpp :: pow c++ 
Cpp :: Maximum element in a map c++ 
Cpp :: memset c++ 
Cpp :: gcd in cpp 
Cpp :: c++ constructor 
Cpp :: conversion of class type data into basic type data in c++ 
Cpp :: ue4 endoverlap c++ 
Cpp :: C++ CHEAT SHEAT 
Cpp :: Common elements gfg in c++ 
Cpp :: c++ *agrs 
Cpp :: print all variables separated by comma c++ 
Cpp :: c create 1 bit value 
Cpp :: c program runner 
Cpp :: texorpdfstring math in title latex 
Cpp :: strong number in c++ 
Cpp :: c plus 
Cpp :: vector with initial size 
Cpp :: cpp class access array member by different name 
Cpp :: c++ system() from variable 
Cpp :: max of 3 numbers in c++ 
Cpp :: C++ selectin file location using Win32 API 
Cpp :: check if a variable is tring c++ 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =