def test_simple(self): testList = [4, 5, 2, 1] expectedList = testList.copy() expectedList.sort() ##If i skip this line, the test go wrong actualList = bs(testList) self.assertEqual(actualList, expectedList)
def test_negative(self): testList = [-4,-3,-2,-1] expectedList = testList.copy() expectedList.sort() actualList = bs(testList) self.assertEqual(actualList, expectedList)
def test_presorted(self): testList = [1,2,3,4] expectedList = testList.copy() expectedList.sort() actualList = bs(testList) self.assertEqual(actualList, expectedList)
def test_empty(self): testList = [] expectedList = testList.copy() expectedList.sort() actualList = bs(testList) self.assertEqual(actualList, expectedList)
from bubbleSort import bubbleSort as bs testList = [1, 5, 3, 2, 1, 5, 6, 7] print(bs(testList))
from bubbleSort import bubbleSort as bs def binarySearch(arr, item): mid = len(arr) // 2 beg = 0 end = len(arr) - 1 while beg < end: print(beg, mid, end, arr[mid]) if arr[mid] == item: return mid if item > arr[mid]: beg = mid + 1 else: end = mid mid = (beg + end) // 2 return -1 if __name__ == "__main__": arr = [5, 3, 4, 2, 0, 1, 8, 7] arr = bs(arr) print(arr) i = binarySearch(arr, 3) print(i)