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

Code of recursive binary search

# Code recursive binary search.
def binarySearchAlgorithm(arr, l, r, x):
	if r >= l:
		mid = l + (r - l) 
		if arr[mid] == x:
			return mid
		elif arr[mid] > x:
			return binarySearch(arr, l, mid-1, x)
		else:
			return binarySearch(arr, mid + 1, r, x)
	else:
		return -1
arr = [3, 43, 56, 96]
find = 56
result = binarySearchAlgorithm(arr, 0, len(arr)-1, find)
if result != -1:
	print("Element is present at index % d" % result)
else:
	print("Element is not present in array")
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

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

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

Code of recursive binary search

# Code recursive binary search.
def binarySearchAlgorithm(arr, l, r, x):
	if r >= l:
		mid = l + (r - l) 
		if arr[mid] == x:
			return mid
		elif arr[mid] > x:
			return binarySearch(arr, l, mid-1, x)
		else:
			return binarySearch(arr, mid + 1, r, x)
	else:
		return -1
arr = [3, 43, 56, 96]
find = 56
result = binarySearchAlgorithm(arr, 0, len(arr)-1, find)
if result != -1:
	print("Element is present at index % d" % result)
else:
	print("Element is not present in array")
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

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 :: #pragma once in main file what is it for 
Cpp :: default rule of five c++ 
Cpp :: c++ get last character of string 
Cpp :: c++ parse int 
Cpp :: quotation in c++ string 
Cpp :: addition without arithmetic operators c++ 
Cpp :: Write C++ program to copy one string to another string using pointers 
Cpp :: function as argument in another function in c++ 
Cpp :: c++ print every element in array 
Cpp :: how to play sound in c++ 
Cpp :: c++ show current time 
Cpp :: arduino notone 
Cpp :: how to make a c++ program which takes two integers and calculate average 
Cpp :: getch c++ library 
Cpp :: c++ lock 
Cpp :: find in set of pairs using first value cpp 
Cpp :: c++ merge sort 
Cpp :: c++ open file 
Cpp :: fiunction in c++ 
Cpp :: how to round to nearest whole number unity 
Cpp :: c++ string to int conversion 
Cpp :: insert vector to end of vector c++ 
Cpp :: how to declare a function in c++ 
Cpp :: c++ max of array 
Cpp :: how to take space separated input in c++ 
Cpp :: how to easily trim a str in c++ 
Cpp :: on component begin overlap c++ 
Cpp :: how to get the type of a variable in c++ 
Cpp :: c++ split string by several space 
Cpp :: for c++ 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =