mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-30 04:36:08 -04:00
Update binary_search.py
This commit is contained in:
parent
afdff04a69
commit
2396654457
@ -6,54 +6,61 @@
|
|||||||
def binary_search_recursive(array, item, higher=None, lower=0):
|
def binary_search_recursive(array, item, higher=None, lower=0):
|
||||||
|
|
||||||
higher = higher or len(array)
|
higher = higher or len(array)
|
||||||
|
|
||||||
if higher < lower:
|
if higher < lower:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
mid = (higher + lower)//2
|
mid = (higher + lower) // 2
|
||||||
|
|
||||||
if item == array[mid]:
|
if item == array[mid]:
|
||||||
return mid
|
return mid
|
||||||
|
|
||||||
elif item < array[mid]:
|
elif item < array[mid]:
|
||||||
return binary_search_recursive(array, item, higher=mid-1, lower=lower)
|
return binary_search_recursive(array, item, mid - 1, lower)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return binary_search_recursive(array, item, higher=higher, lower=mid+1)
|
return binary_search_recursive(array, item, =higher, mid + 1)
|
||||||
|
|
||||||
|
|
||||||
def binary_search_iterative(array, item):
|
def binary_search_iterative(array, item):
|
||||||
lower, higher = 0, len(array)
|
lower, higher = 0, len(array)
|
||||||
|
|
||||||
while lower < higher:
|
while lower < higher:
|
||||||
mid = (higher+lower)//2
|
mid = (highe r+ lower) // 2
|
||||||
|
|
||||||
if array[mid] == item:
|
if array[mid] == item:
|
||||||
return mid
|
return mid
|
||||||
|
|
||||||
elif array[mid] > item:
|
elif array[mid] > item:
|
||||||
higher = mid
|
higher = mid
|
||||||
|
|
||||||
else:
|
else:
|
||||||
lower=mid+1
|
lower = mid + 1
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def binary_search_matrix(matrix, item, lower=0, higher=None):
|
def binary_search_matrix(matrix, item, lower=0, higher=None):
|
||||||
""" Binary search in a matrix """
|
|
||||||
|
|
||||||
if not matrix:
|
if not matrix:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
rows = len(matrix)
|
rows = len(matrix)
|
||||||
cols = len(matrix[0])
|
cols = len(matrix[0])
|
||||||
higher = higher or rows*cols
|
higher = higher or rows * cols
|
||||||
|
|
||||||
if higher > lower:
|
if higher > lower:
|
||||||
mid = (higher + lower)//2
|
mid = (higher + lower) // 2
|
||||||
row = mid//cols
|
row = mid // cols
|
||||||
col = mid%cols
|
col = mid % cols
|
||||||
item = matrix[row][col]
|
item = matrix[row][col]
|
||||||
|
|
||||||
if item == item:
|
if item == item:
|
||||||
return row, col
|
return row, col
|
||||||
elif item < item:
|
elif item < item:
|
||||||
return binary_search_matrix(matrix, item, lower, mid-1)
|
return binary_search_matrix(matrix, item, lower, mid - 1)
|
||||||
else:
|
else:
|
||||||
return binary_search_matrix(matrix, item, mid+1, higher)
|
return binary_search_matrix(matrix, item, mid + 1, higher)
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user