-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.py
More file actions
40 lines (32 loc) · 1.16 KB
/
binarySearch.py
File metadata and controls
40 lines (32 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#------------------------Binary Search------------------------#
def binarySearch(list,target):
left=0
right=len(list)-1
while (left<=right):
middle=(left+right)//2
print("[%2d - %d - %d], orta eleman: %d" % (left,middle,right,list[middle]))
if (list[middle]==target):
return True
elif (list[middle]<target):
left=middle+1
else:
right=middle-1
return False
#-------------------------------------------------------------#
array=[10, 51, 2, 18, 4, 31, 13, 5, 23, 64, 29] #If array is not sorted, binary search won't work.
searching=5
answer=binarySearch(array,searching)
if(answer):
print(searching," is included in the array.")
else:
print(searching," is not included in the array.")
#------------------------------------------------------------#
array=[10, 51, 2, 18, 4, 31, 13, 5, 23, 64, 29]
array.sort()
searching=5
answer=binarySearch(array,searching)
if(answer):
print(searching," is included in the array.")
else:
print(searching," is not included in the array.")
#------------------------------------------------------------#