8000 binary_search_algorith · brohimah/programminginpython.com@b377a99 · GitHub
[go: up one dir, main page]

Skip to content

Commit b377a99

Browse files
committed
binary_search_algorith
1 parent 1972d52 commit b377a99

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
__author__ = 'Avinash'
2+
3+
4+
def binary_sort(sorted_list, length, key):
5+
start = 0
6+
end = length-1
7+
while start <= end:
8+
mid = int((start + end)/2)
9+
if key == sorted_list[mid]:
10+
print("\nEntered number %d is present "
11+
"at position: %d" % (key, mid))
12+
return -1
13+
elif key < sorted_list[mid]:
14+
end = mid - 1
15+
elif key > sorted_list[mid]:
16+
start = mid + 1
17+
print("\nElement not found!")
18+
return -1
19+
20+
lst = []
21+
22+
size = int(input("Enter size of list: \t"))
23+
24+
for n in range(size):
25+
numbers = int(input("Enter any number: \t"))
26+
lst.append(numbers)
27+
28+
lst.sort()
29+
print('\n\nThe list will be sorted, the sorted list is:', lst)
30+
31+
x = int(input("\nEnter the number to search: "))
32+
33+
binary_sort(lst, size, x)

0 commit comments

Comments
 (0)
0