-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW-05.py
60 lines (48 loc) · 1.41 KB
/
HW-05.py
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
51
52
53
54
55
56
57
58
59
60
array = [int(x) for x in input("Введи числа через пробел").split()]
def merge_sort(array):
if len(array) < 2:
return array[:]
else:
middle = len(array) // 2
left = merge_sort(array[:middle])
right = merge_sort(array[middle:])
return merge(left, right)
def merge(left, right):
result = []
i, j = 0, 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
while i < len(left):
result.append(left[i])
i += 1
while j < len(right):
result.append(right[j])
j += 1
return result
print(merge_sort(array))
def binary_search(array, element, left, right):
if left > right:
return False
middle = (right + left) // 2
if array[middle] == element:
return middle
elif element < array[middle]:
return binary_search(array, element, left, middle - 1)
else:
return binary_search(array, element, middle + 1, right)
while True:
try:
element = int(input("Число:"))
if element < 0 or element > 999:
raise Exception
break
except ValueError:
print("Введи число")
except Exception:
print("Неверно")
print(binary_search(array, element, 0, len(array)))