Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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

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
Python :: python string cut 
Python :: python string cut first n characters 
Python :: finding the maximum value in a list python 
Python :: series.string.split expand 
Python :: python insert path 
Python :: how to check if a string contains a word python 
Python :: python help 
Python :: django message 
Python :: python max function recursive 
Python :: glob python 
Python :: how to download packages using pip 
Python :: a string starts with an uppercase python 
Python :: python remove common elements between two lists 
Python :: from django.db import models 
Python :: python repet x time 
Python :: python i++ 
Python :: accessing items of tuple in python 
Python :: install json on python 
Python :: docker build python fastapi 
Python :: django sample 
Python :: pandas df represent a long column name with short name 
Python :: python function with two parameters 
Python :: stutter function in python 
Python :: unsupervised learning 
Python :: functools reduce python 
Python :: get channle from id discord.py 
Python :: postman authorization 
Python :: reverse function python 
Python :: python start process in background and get pid 
Python :: optimizationed bubble sort optimizationed 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =