# Binary search
list1 = [11,22,13,44,65,56,87,88,79,10]
list1.sort()
num = int(input("Enter element to search : "))
bottom = 0
top = len(list1)-1
while(True):
if (bottom > top):
print("Element not found")
break
middle = (bottom+top)//2
if(list1[middle] == num):
print("Sorted list :", list1)
print("Element", num, "present at index", middle)
break
elif (list1[middle] < num):
bottom = middle + 1
else:
top = middle - 1
list1 = [11,22,13,44,65,56,87,88,79,10]
list1.sort()
num = int(input("Enter element to search : "))
bottom = 0
top = len(list1)-1
while(True):
if (bottom > top):
print("Element not found")
break
middle = (bottom+top)//2
if(list1[middle] == num):
print("Sorted list :", list1)
print("Element", num, "present at index", middle)
break
elif (list1[middle] < num):
bottom = middle + 1
else:
top = middle - 1
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu