Created
September 9, 2021 01:42
-
-
Save aliifam/5ba16f8cc1c41a6a76ab5c3a2075a574 to your computer and use it in GitHub Desktop.
simple binary search algorithtm implemented with python language
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #binanry searching algorithm | |
| item = int(input()) | |
| def binary_search(list, item): #parameter is a item want we search and list where the item located | |
| low = 0 #index of first element in list | |
| high = len(list) - 1 #index of last element in list | |
| while low <= high: | |
| mid = (low + high) // 2 | |
| guess = list[mid] | |
| if guess == item: | |
| return mid | |
| if guess > item: | |
| high = mid - 1 | |
| else: | |
| low = mid + 1 | |
| return None | |
| my_list = [1, 3, 5, 7, 9] | |
| print(binary_search(my_list, item), "its index element location in list") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment