实现拥有重复元素的二分查找法
来源:互联网 发布:林非比淘宝模特 编辑:程序博客网 时间:2024/06/05 10:49
github 莫名其妙的打不开了,那就贴在这里吧
#!/usr/bin/env python# -*- #coding:utf-8'''Created on 2013-9-4@author: mingdong.li二分查找法,有相同元素列表'''import unittestfrom test.test_bisect import TestErrorHandlingfrom test import test_support_searchMode = {"FIRST":1, "LAST":0}def binarySearchTemplate(sequence, fromIndex, toIndex, key, searchMode): """ @param sequence: is a list or tuple to be searched, it's sequential and no repeating elements @param fromIndex: the index of the first element to be searched @param toIndex: the index of the last element to be searched @param key: the value to be searched for @param searchMode: FIRST=1(return the index of the first element in the repeating sequence); LAST=0(the last one) @return: index of the search key, no found will return -1 """ low = fromIndex high = toIndex - 1 flag = -1 while(low <= high): mid = low + ((high-low)>>1) midValue = sequence[mid] if midValue < key: low = mid + 1 elif midValue > key: high = mid -1 else: if searchMode is None: return mid elif searchMode == _searchMode['FIRST']: high = mid - 1 flag = mid elif searchMode == _searchMode['LAST']: low = mid + 1 flag = mid else: if searchMode == _searchMode['FIRST']: if key == sequence[high]:# print 'first high' return high else:# print 'first flag' return flag elif searchMode == _searchMode['LAST']: if key == sequence[low-1]:# print 'last high' return low-1 else:# print 'last flag' return flag else: return -1# return -1def binarySearch(sequence, key, searchMode=None): if not isinstance(sequence, list) and not isinstance(sequence, tuple): return "The %s is not list or tuple" % sequence return binarySearchTemplate(sequence, 0, len(sequence), key, searchMode) def test_main(verbose=None): class TestBinarySearch(unittest.TestCase): a = [1, 1, 1, 2, 2, 3, 4, 4, 4, 5, 6, 7, 7, 7, 7, 7] b = (1, 1, 1, 2, 2, 3, 4, 4, 4, 5, 6, 7, 7) precomputedCase = [ (binarySearch, 'a', 88, None, "The a is not list or tuple"), (binarySearch, a, 2, None, 3), (binarySearch, a, 88, None, -1), (binarySearch, a, 7, _searchMode['FIRST'], 11), (binarySearch, a, 7, _searchMode['LAST'], 15), (binarySearch, a, 1, _searchMode['FIRST'], 0), (binarySearch, a, 1, _searchMode['LAST'], 2), (binarySearch, a, 4, _searchMode['FIRST'], 6), (binarySearch, a, 11, _searchMode['FIRST'], -1), (binarySearch, a, 11, _searchMode['LAST'], -1), (binarySearch, b, 2, None, 4), (binarySearch, b, 88, None, -1), (binarySearch, b, 7, _searchMode['FIRST'], 11), (binarySearch, b, 7, _searchMode['LAST'], 12), (binarySearch, b, 1, _searchMode['FIRST'], 0), (binarySearch, b, 1, _searchMode['LAST'], 2), (binarySearch, b, 4, _searchMode['FIRST'], 6), (binarySearch, b, 11, _searchMode['FIRST'], -1), (binarySearch, b, 11, _searchMode['LAST'], -1) ] def test_precomputed(self): for func, sequ, key, mode, expected in self.precomputedCase: self.assertEqual(func(sequ, key, mode), expected) from types import BuiltinFunctionType test_classes = [TestBinarySearch] if isinstance(binarySearch, BuiltinFunctionType): test_classes.append(TestErrorHandling) test_support.run_unittest(*test_classes)if __name__ == '__main__': print "test..." test_main(verbose=True)
- 实现拥有重复元素的二分查找法
- 含有重复元素的二分查找算法
- Java实现二分查找算法(元素可重复)
- 带有重复元素的有序数组二分查找
- 二分查找法的元素查找次数求解
- 查找数组重复的元素
- 顺序元素的二分查找
- Leetcode|Find Minimum in Rotated Sorted Array II(有重复元素的二分查找)
- 二分查找的实现
- 二分查找的实现
- 二分查找的实现
- 二分查找的实现
- 二分查找的实现
- 二分查找的实现
- 二分查找的实现
- 二分查找的实现
- 二分查找的实现
- 【查找】找出两个数组共同拥有的元素
- poj 1742 多重背包变种 对时间要求特别严格
- SQL2005CLR函数扩展-字符串函数
- python发送post请求
- Using the PopoverView in iPad App Development
- 126页E2
- 实现拥有重复元素的二分查找法
- Mac 上面编译POCO C++库
- 条码十六进制数据转换成字符串的SQL_CLR代码
- C# DEBUG 调试信息打印及输出详解
- PostgreSQL 9.2.4 命令行帮助及SQL语句简要说明
- strcpy完整版 与 strcpy为什么有返回值
- java web从零单排第九期《struts2》action的转发与重定向
- 设备驱动程序学习笔记(1)
- Ext学习笔记(6):FormPanel 的Demo