Algorithms-and-Data-Structures/Python-Binary-Search/binarySearch.py

22 lines
442 B
Python
Raw Normal View History

2024-05-02 20:14:36 +01:00
nums = [6, 7, 8, 12, 19, 55, 90, 98];
searchItem = 55;
found = False;
start = 0;
end = len(nums);
mid = 0;
while (start <= end):
mid = int((start + end) / 2);
if nums[mid] == searchItem:
print(f'found {searchItem} at index {mid}');
found = True;
break;
elif nums[mid] > searchItem:
end = mid - 1;
else:
start = mid + 1
if not found:
print(f'{searchItem} was not found in nums');