|
| 1 | +number_list = [0, 1, 2, 3, 4, 5, 6, 7] |
| 2 | + |
| 3 | + |
| 4 | +def linear_search(value, array): |
| 5 | + for index, val in enumerate(array): |
| 6 | + if val == value: |
| 7 | + return index |
| 8 | + return -1 |
| 9 | + |
| 10 | + |
| 11 | +assert linear_search(5, number_list) == 5 |
| 12 | + |
| 13 | + |
| 14 | +def linear_search_recusive(value, array): |
| 15 | + if len(array) == 0: |
| 16 | + return -1 |
| 17 | + index = len(array) - 1 |
| 18 | + if array[index] == value: |
| 19 | + return index |
| 20 | + else: |
| 21 | + return linear_search_recusive(value, array[0:index]) |
| 22 | + |
| 23 | + |
| 24 | +assert linear_search_recusive(5, number_list) == 5 |
| 25 | +assert linear_search_recusive(8, number_list) == -1 |
| 26 | +assert linear_search_recusive(7, number_list) == 7 |
| 27 | +assert linear_search_recusive(0, number_list) == 0 |
| 28 | + |
| 29 | + |
| 30 | +def binary_search(value, array): |
| 31 | + if not array: |
| 32 | + return -1 |
| 33 | + |
| 34 | + beg = 0 |
| 35 | + end = len(array) - 1 |
| 36 | + |
| 37 | + while beg <= end: |
| 38 | + mid = int((beg + end) / 2) |
| 39 | + if array[mid] == value: |
| 40 | + return mid |
| 41 | + elif array[mid] > value: |
| 42 | + end = mid - 1 |
| 43 | + else: |
| 44 | + beg = mid + 1 |
| 45 | + return -1 |
| 46 | + |
| 47 | + |
| 48 | +def test_binary_search(): |
| 49 | + a = list(range(10)) |
| 50 | + |
| 51 | + # 正常值 |
| 52 | + assert binary_search(-1, a) == -1 |
| 53 | + assert binary_search(1, a) == 1 |
| 54 | + |
| 55 | + # 异常值 |
| 56 | + assert binary_search(1, None) == -1 |
| 57 | + |
| 58 | + # 边界值 |
| 59 | + assert binary_search(0, a) == 0 |
| 60 | + |
| 61 | + |
| 62 | +test_binary_search() |
| 63 | + |
| 64 | + |
| 65 | +def binary_search_recusive(value, array): |
| 66 | + if not array: |
| 67 | + return -1 |
| 68 | + |
| 69 | + beg = 0 |
| 70 | + end = len(array) - 1 |
| 71 | + mid = int((beg + end) / 2) |
| 72 | + if mid == value: |
| 73 | + return mid |
| 74 | + elif array[mid] > value: |
| 75 | + return binary_search_recusive(value, array[0:mid]) |
| 76 | + else: |
| 77 | + return binary_search_recusive(value, array[mid:end]) |
| 78 | + |
| 79 | + |
| 80 | +def test_binary_search_recusive(): |
| 81 | + a = list(range(10)) |
| 82 | + |
| 83 | + # 正常值 |
| 84 | + assert binary_search_recusive(-1, a) == -1 |
| 85 | + assert binary_search_recusive(1, a) == 1 |
| 86 | + |
| 87 | + # 异常值 |
| 88 | + assert binary_search_recusive(1, None) == -1 |
| 89 | + |
| 90 | + # 边界值 |
| 91 | + assert binary_search_recusive(0, a) == 0 |
| 92 | + |
| 93 | + |
| 94 | +test_binary_search_recusive() |
| 95 | + |
| 96 | + |
| 97 | +def bisect_own(value, array): |
| 98 | + if not array: |
| 99 | + return -1 |
| 100 | + |
| 101 | + lo = 0 |
| 102 | + end = len(array) |
| 103 | + |
| 104 | + while lo < end: |
| 105 | + mid = int((lo + end) / 2) |
| 106 | + if array[mid] < value: |
| 107 | + lo = mid + 1 |
| 108 | + else: |
| 109 | + end = mid |
| 110 | + return lo |
| 111 | + |
| 112 | + |
| 113 | +def test_bisect_own(): |
| 114 | + a = list(range(10)) |
| 115 | + |
| 116 | + # 正常值 |
| 117 | + assert bisect_own(-1, a) == 0 |
| 118 | + assert bisect_own(11, a) == 10 |
| 119 | + |
| 120 | + |
| 121 | +test_bisect_own() |
0 commit comments