-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.py
More file actions
50 lines (48 loc) · 1.29 KB
/
binary.py
File metadata and controls
50 lines (48 loc) · 1.29 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
41
42
43
44
45
46
47
48
49
50
def file(path):
def mergeSort(arr):
if len(arr) > 1:
mid = len(arr) // 2
L = arr[:mid]
R = arr[mid:]
mergeSort(L)
mergeSort(R)
i = j = k = 0
while i < len(L) and j < len(R):
if L[i] < R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
while i < len(L):
arr[k] = L[i]
i += 1
k += 1
while j < len(R):
arr[k] = R[j]
j += 1
k += 1
return arr
fhand = open(path, 'r')
str = ''
for line in fhand:
str += line
lst = str.split()
lst = mergeSort(lst)
return lst
def binarySearch(lst, l, r,x):
if r>=l:
mid = l+ int((r-l)/2)
#check if x is present at mid
if lst[mid]==x:
return True
#if x is greater, ignore left half
elif lst[mid] >x:
return(binarySearch(lst, l, mid-1,x))
# if x is smaller, ignore right half
else:
return(binarySearch(lst, mid+1,r,x))
else:
#if we reach here then the element was not present
return False