- #Python program for Linear Search
- #create array to store all the numbers
- myList = [1,2,4,5,7,8,10,12,15,21,27,29,30]
- #enter item to search for
- item = int(input("Please enter item to be found "))
- found = False
- upperBound = 12
- lowerBound = 0
- while (not found) and (lowerBound!=upperBound):
- index = int((upperBound + lowerBound)/2)
- if(myList[index] == item):
- found = True
- if item > myList[index]:
- lowerBound = index
- if item < myList[index]:
- upperBound = index
- if(found):
- print("Item found")
- else:
- print("Item not found")
程序段落的分享实例 BinarySearch.py
于 2022-01-05 13:53:27 首次发布