mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
👾
This commit is contained in:
parent
1d44d182e2
commit
a85ed914d3
320 changed files with 0 additions and 0 deletions
70
searching/binary_search.py
Normal file
70
searching/binary_search.py
Normal file
|
@ -0,0 +1,70 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# author: bt3gl
|
||||
|
||||
|
||||
def binary_search_recursive(array, item, higher=None, lower=0):
|
||||
|
||||
higher = higher or len(array)
|
||||
if higher < lower:
|
||||
return False
|
||||
|
||||
mid = (higher + lower)//2
|
||||
if item == array[mid]:
|
||||
return mid
|
||||
elif item < array[mid]:
|
||||
return binary_search_recursive(array, item, higher=mid-1, lower=lower)
|
||||
else:
|
||||
return binary_search_recursive(array, item, higher=higher, lower=mid+1)
|
||||
|
||||
|
||||
def binary_search_iterative(array, item):
|
||||
lower, higher = 0, len(array)
|
||||
|
||||
while lower < higher:
|
||||
mid = (higher+lower)//2
|
||||
if array[mid] == item:
|
||||
return mid
|
||||
elif array[mid] > item:
|
||||
higher = mid
|
||||
else:
|
||||
lower=mid+1
|
||||
return False
|
||||
|
||||
|
||||
def binary_search_matrix(matrix, item, lower=0, higher=None):
|
||||
""" Binary search in a matrix """
|
||||
|
||||
if not matrix:
|
||||
return None
|
||||
|
||||
rows = len(matrix)
|
||||
cols = len(matrix[0])
|
||||
higher = higher or rows*cols
|
||||
|
||||
if higher > lower:
|
||||
mid = (higher + lower)//2
|
||||
row = mid//cols
|
||||
col = mid%cols
|
||||
item = matrix[row][col]
|
||||
|
||||
if item == item:
|
||||
return row, col
|
||||
elif item < item:
|
||||
return binary_search_matrix(matrix, item, lower, mid-1)
|
||||
else:
|
||||
return binary_search_matrix(matrix, item, mid+1, higher)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
array = [2, 3, 5, 6, 8, 10, 15, 23]
|
||||
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
|
||||
item = 15
|
||||
|
||||
print('Recursive: ', binary_search_recursive(array, item))
|
||||
print('Iterative: ', binary_search_iterative(array, item))
|
||||
print('Matrix: ', binary_search_matrix(matrix, item))
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue