-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearching.py
99 lines (82 loc) · 3.27 KB
/
Searching.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from typing import List, TypeVar
from utils import Comparable
T = TypeVar(name="T", bound=Comparable)
def OrderedSequentialSearch(arr: List[T], item: T, asc: bool = True) -> bool: # type: ignore
"""有序数组的顺序查找
Args:
arr (List[T]): 待查询的有序数组
item (T): 待查询元素
asc (bool, optional): 有序数组是否是升序. Defaults to True.
Returns:
bool: 待查询元素在有序数组中是否存在
"""
if asc: # 升序数组
for i in range(0, len(arr), 1):
if item > arr[i]:
continue
elif item < arr[i]:
return False
else: # item == arr[i]
return True
else: # 降序数组
for i in range(len(arr) - 1, -1, -1):
if arr[i] < item:
continue
elif arr[i] > item:
return False
else: # arr[i] == item
return True
def BinarySearch(arr: List[T], item: T, asc: bool = True) -> bool:
"""有序数组的二分查找"""
first: int = 0
last: int = len(arr) - 1
if asc: # 升序数组
while first <= last:
middle: int = (first + last) // 2
if item < arr[middle]:
last = middle - 1
elif item > arr[middle]:
first = middle + 1
else: # item == arr[middle]
return True
else: # 降序数组
while first <= last:
middle: int = (first + last) // 2
if item < arr[middle]:
first = middle + 1
elif item > arr[middle]:
last = middle - 1
else: # item == arr[middle]
return True
return False
def RecursiveBinarySearch(arr: List[T], item: T, first: int, last: int, asc: bool = True) -> bool:
"""二分查找的递归版本"""
if first > last:
return False
middle: int = (last + first) // 2
if asc: # 升序数组
if item < arr[middle]:
return RecursiveBinarySearch(arr=arr, item=item, first=first, last=middle-1, asc=asc)
elif item > arr[middle]:
return RecursiveBinarySearch(arr=arr, item=item, first=middle+1, last=last, asc=asc)
else: # item == arr[middle]
return True
else: # 降序数组
if item > arr[middle]:
return RecursiveBinarySearch(arr=arr, item=item, first=first, last=middle-1, asc=asc)
elif item < arr[middle]:
return RecursiveBinarySearch(arr=arr, item=item, first=middle+1, last=last, asc=asc)
else: # item == arr[middle]
return True
if __name__ == "__main__":
from Sorting import quick_sort
arr: List[int] = [49, 99, 92, 121, 31, 96, 132, 145, 169, 0, 158]
quick_sort(arr=arr, left=0, right=len(arr)-1, asc=False)
item: int = 9
print(OrderedSequentialSearch(arr=arr, item=item, asc=False))
print(BinarySearch(arr=arr, item=item, asc=False))
print(RecursiveBinarySearch(arr=arr, item=item, first=0, last=len(arr)-1, asc=False))
item2: int = 96
print(OrderedSequentialSearch(arr=arr, item=item2, asc=False))
print(BinarySearch(arr=arr, item=item2, asc=False))
print(RecursiveBinarySearch(arr=arr, item=item2, first=0, last=len(arr)-1, asc=False))