-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lecture "Divide and conquer algorithms", exercise 1 #26
Labels
Comments
# Test case for the algorithm
def test_binary_search(item, ordered_list, start, end, expected):
result = binary_search(item, ordered_list, start, end)
if result == expected:
return True
else:
return False
# Code for the algorithm
def binary_search(item, ordered_list, start, end):
if end >= start:
mid = (start+end) // 2
if ordered_list[mid] == item:
return mid
elif ordered_list[mid] < item:
return binary_search(item, ordered_list, mid+1, end)
else:
return binary_search(item, ordered_list, start, mid-1)
else:
return None
# Three test runs
print(test_binary_search(4, [1, 2, 3, 4, 5 ,6], 0, 5, 3)) #True
print(test_binary_search(1, [2, 3, 4, 5], 0, 3, None)) #True
print(test_binary_search(1, [1], 0, 0, 0)) #True |
|
def test_binary_search(item, ordered_list, start, end, expected_result):
result = binary_search(item, ordered_list, start, end)
return result == expected_result
def binary_search(item, ordered_list, start, end):
middle_item = ordered_list[(start + end) // 2]
if item in ordered_list[start:end + 1]:
if middle_item == item:
return ordered_list.index(middle_item)
elif middle_item < item:
return binary_search(item, ordered_list, ordered_list.index(middle_item) + 1, end)
elif middle_item > item:
return binary_search(item, ordered_list, start, ordered_list.index(middle_item) - 1)
else:
return None
print(test_binary_search(3 , [1, 2, 3, 4, 5, 6, 7, 8, 9], 2, 6, 2))
print(test_binary_search('b', ['a', 'b', 'c', 'd', 'e', 'f'], 1, 3, 1))
print(test_binary_search(4, [1, 2, 3, 4, 5 ,6], 4, 5, None))
print(test_binary_search(1, [2, 3, 4, 5], 0, 3, None))
print(test_binary_search(1, [1], 0, 0, 0)) |
def test_binary_search(item, ordered_list, start, end, expected):
if expected == binary_search(item, ordered_list, start, end):
return True
else:
return False
def binary_search(item, ordered_list, start, end):
while len(ordered_list) > 0 and start <= end:
mid = (start + end) // 2
if item == ordered_list[mid]:
return mid
elif ordered_list[mid] < item:
return binary_search(item, ordered_list, mid + 1, end)
else:
return binary_search(item, ordered_list, start, mid - 1)
print(test_binary_search(4, [0, 2, 3, 5, 7, 8, 9], 0, 6, None)) # True
print(test_binary_search(8, [0, 2, 3, 5, 7, 8, 9], 0, 6, 5)) # True
print(test_binary_search(8, [0, 2, 3, 5, 7, 8, 9], 0, 3, None)) # True
print(test_binary_search(8, [], 0, 3, None)) # True
print(test_binary_search('f', ['a', 'b', 'c', 'd', 'e', 'f'], 0, 5, 5)) # True |
def test_binary_search(item, ordered_list, start, end, expected):
result = binary_search(item, ordered_list, start, end)
return result == expected
def binary_search(item, ordered_list, start, end):
if start<=end:
mid = (start+end) // 2
if ordered_list[mid] == item:
return mid
elif ordered_list[mid] < item:
return binary_search(item, ordered_list, start+1, end)
elif ordered_list[mid] > item:
return binary_search(item, ordered_list, start, end-1)
else:
return None
print(test_binary_search("Bologna", ["Bologna", "Firenze", "Milano", "Roma"], 0, 3, 0)) #True
print(test_binary_search("Milano", ["Bologna", "Firenze", "Milano", "Roma"], 0, 3, 2)) #True
print(test_binary_search(2, [0, 1, 2, 3, 4, 5], 0, 5, 2)) #True
print(test_binary_search("Palermo", ["Bologna", "Firenze", "Milano", "Roma"], 0, 3, None)) #True |
|
|
def binary_search(item, ordered_list, start, end):
mid = (start+end)//2
if start <= end:
if ordered_list[mid] == item:
return mid
elif ordered_list[mid] < item:
start = mid + 1
return binary_search(item, ordered_list, start, end)
elif ordered_list[mid] > item:
end = mid - 1
return binary_search(item, ordered_list, start, end)
else:
return None
print(test_binary_search("d", ["a", "b", "c", "d", "e", "f"], 0, 5, 3))
print(test_binary_search("d", ["a", "b", "c", "d", "d"], 0, 4, 3))
print(test_binary_search("d", ["a", "b", "c", "c", "e"], 1, 4, None))
print(test_binary_search(5, [0, 1, 1, 2, 5, 7], 3, 5, 4)) |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Implement the binary search algorithm in Python – i.e. the recursive function
def binary_search(item, ordered_list, start, end)
. It takes an item to search (i.e. item), an ordered list and a starting and ending positions in the list as input. It returns the position ofitem
in the list if it is in it and None otherwise. The binary search first checks if the middle item of the list between start and end (included) is equal toitem
, and returns its position in this case. Otherwise, if the central item is less thanitem
, the algorithm continues the search in the part of the list that follows the middle item. Instead, if the central item is greater thanitem
, the algorithm continues the search in the part of the list preceding the central item. Accompany the implementation of the function with the appropriate test cases. As supporting material, Fekete and Morr released a nonverbal definition of the algorithm [Fekete, Morr, 2018a], which is useful to understand the rationale of the binary search steps.The text was updated successfully, but these errors were encountered: