|
1 | 1 | #!/usr/bin/env python
|
2 | 2 | # -*- coding: utf-8 -*-
|
3 | 3 | # Created by imoyao at 2019/5/13 18:33
|
| 4 | +import os |
| 5 | +import sys |
4 | 6 |
|
| 7 | +try: |
| 8 | + from ..util import utils |
| 9 | +except ValueError: |
| 10 | + currpath = os.path.join(os.getcwd(), os.path.dirname(__file__)) |
| 11 | + relative_fp = os.path.dirname(currpath) |
| 12 | + print(relative_fp) |
| 13 | + sys.path.append(relative_fp) |
| 14 | + from util import utils |
5 | 15 |
|
6 |
| -def binary_search(sorted_lists, wanted_item): |
| 16 | + |
| 17 | +@utils.show_time |
| 18 | +def binary_search_while(sorted_lists, wanted_item): |
| 19 | + """ |
| 20 | + 二分查找( while 实现) |
| 21 | + :param sorted_lists: 有序数组 |
| 22 | + :param wanted_item: 需要查找的元素 |
| 23 | + :return: 索引值 / None |
| 24 | + """ |
7 | 25 | low_index = 0
|
8 |
| - high_index = len(sorted_lists) - 1 |
9 |
| - count = 0 |
| 26 | + high_index = len(sorted_lists) - 1 # 查找范围为整个有序列表的索引 |
| 27 | + _count = 0 |
10 | 28 | while low_index <= high_index:
|
11 |
| - count += 1 |
12 |
| - mid_index = (low_index + high_index) // 2 |
13 |
| - # print(mid_index) |
| 29 | + _count += 1 |
| 30 | + mid_index = (low_index + high_index) // 2 # py3中的整数除 |
14 | 31 | mid_item = sorted_lists[mid_index]
|
15 | 32 | if mid_item == wanted_item:
|
16 |
| - print('It takes {0} times to find {1} in index {2}'.format(count, wanted_item, mid_index)) |
17 |
| - return mid_item |
| 33 | + print('It takes {0} times to find {1} in index {2}'.format(_count, wanted_item, mid_index)) |
| 34 | + return mid_index |
| 35 | + elif mid_item < wanted_item: # 如果猜的值小于目的值,则在大端继续查找 |
| 36 | + low_index = mid_index + 1 |
18 | 37 | else:
|
19 |
| - if mid_item < wanted_item: |
20 |
| - low_index = mid_index + 1 |
21 |
| - else: |
22 |
| - high_index = mid_index - 1 |
23 |
| - return None |
| 38 | + high_index = mid_index - 1 # 否则,在小端查找 |
| 39 | + return None # 没有找到,返回None |
| 40 | + |
| 41 | + |
| 42 | +count = 0 |
| 43 | + |
| 44 | + |
| 45 | +@utils.show_time |
| 46 | +def binary_search_recurse(sorted_lists, wanted_item, low_index=0, high_index=None): |
| 47 | + """ |
| 48 | + 二分查找(递归实现) |
| 49 | + :param sorted_lists: 有序序列 |
| 50 | + :param wanted_item: 查找的元素 |
| 51 | + :param low_index: 开始位索引 |
| 52 | + :param high_index: 结束位索引 |
| 53 | + :return: 索引值 / None |
| 54 | + """ |
| 55 | + global count |
| 56 | + |
| 57 | + if high_index is None: |
| 58 | + high_index = len(sorted_lists) - 1 |
| 59 | + |
| 60 | + mid_index = (low_index + high_index) // 2 |
| 61 | + mid_item = sorted_lists[mid_index] |
| 62 | + count += 1 |
| 63 | + if low_index > high_index: # 注意此处,表示遍历序列,也没找到 |
| 64 | + return None |
| 65 | + if mid_item < wanted_item: # 猜值小于目的值,则在大端继续查找 |
| 66 | + return binary_search_recurse(sorted_lists, wanted_item, mid_index + 1, high_index) |
| 67 | + elif mid_item > wanted_item: # 小端查找 |
| 68 | + return binary_search_recurse(sorted_lists, wanted_item, low_index, mid_index - 1) |
| 69 | + |
| 70 | + print('It takes {0} times to find {1} in index {2}'.format(count, wanted_item, mid_index)) |
| 71 | + return mid_index |
24 | 72 |
|
25 | 73 |
|
26 | 74 | if __name__ == '__main__':
|
27 |
| - a = binary_search(list(range(100)), 50) |
28 |
| - print('ret:', a) |
| 75 | + print(binary_search_while(list(range(100)), 0)) |
| 76 | + print(binary_search_recurse(list(range(100)), 0, 0, len(list(range(100))) - 1)) |
0 commit comments