Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import time
- numbers=[]
- x=0
- y=50
- for num in range(16):
- start = time.time()
- temp = random.randint(x,y)
- numbers.append(temp)
- x+=50
- y+=50
- end = time.time()
- print(numbers)
- print(len(numbers))
- def binarySearch():
- result = -1
- first=0
- last=len(numbers)
- count=0
- wanted=int(input("Enter your wanted value"))
- while (first <= last and result==-1):
- count+=1
- mid = int((first+last)/2)
- if (wanted == numbers[mid]):
- result=mid
- print("found!!")
- else:
- if (wanted<numbers[mid]):
- last = mid - 1
- else:
- first = mid+1
- end = time.time()
- print("item found at index position",result," Time:",end-start,"count ",count)
- result = -1
- first=0
- last=len(numbers)
- count=0
- def sequentialSearch():
- count=0
- wanted=int(input("Enter your wanted value"))
- start = time.time()
- for x in range (len(numbers)):
- if numbers[x]==wanted:
- end = time.time()
- print("item found at index position",numbers[x]," Time:",end-start,"count ",count)
- break
- else:
- count+=1
- while (True):
- choice=input("A: Sequential Search or B: Binary search?")
- if choice =="A":
- sequentialSearch()
- elif choice =="B":
- binarySearch()
- else:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement