top of page

Search Algorithms in Python


1. Binary search


def binary_search(data, target, low, high):
    if low > high:
        return False
    else:
        mid = (low+high)//2
        if target == data[mid]:
            return True
        elif target < data[mid]:
            return binary_search(data, target, low, mid-1)
        else:
            return binary_search(data, target, mid+1, high)
        
data = [3,4,5,6,7,9,10,34,34,65,77]
binary_search(data, 34, 0, 11)

True



(TO BE CONTINUED)




Recent Posts

See All
Sorting Algorithms in Python

1. Selection sort Method 1 def selectionSort(array): n = len(array) for i in range(n): # Initially, assume the first element of the...

 
 
 

Comentarios


bottom of page