-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter4.py
More file actions
77 lines (59 loc) · 1.43 KB
/
Copy pathchapter4.py
File metadata and controls
77 lines (59 loc) · 1.43 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 第四章,减治法
def binary_search(arr: list, k):
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == k:
return mid
elif arr[mid] > k:
high = mid - 1
else:
low = mid + 1
return -1
class BinaryNode:
def __init__(self, data=0, left=None, right=None):
self.data = data
self.left = left
self.right = right
def bst_search(root: BinaryNode, k):
if not root:
return None
elif root.data == k:
return root
elif root.data > k:
return bst_search(root.left, k)
else:
return bst_search(root.right, k)
def insert_sort(arr: list):
for i in range(1, len(arr)):
temp = arr[i]
j = i
while j > 0 and temp < arr[j - 1]:
arr[j] = arr[j - 1]
j -= 1
arr[j] = temp
def heap_sort(arr: list):
# todo
pass
# 选择arr中第k小的元素作为返回值
def select_problem(arr: list, n):
arr.sort()
return arr[n - 1]
class Player:
def __init__(self):
pass
# champion_game调用
def compete(p1: Player, p2: Player) -> bool:
pass
# 淘汰赛冠军问题
def champion_game(r: list):
n = len(r)
if not n:
return None
while n > 1:
n = n // 2
for i in range(n):
if compete(r[i + n], r[i]):
r[i] = r[i + n]
return r[0]