1+ #
2+ # Binary search works for a sorted array.
3+ # Note: The code logic is written for an array sorted in
4+ # increasing order.
5+ # T(n): O(log n)
6+ #
17
28
39def binary_search (array , query ):
@@ -13,14 +19,19 @@ def binary_search(array, query):
1319 hi = mid - 1
1420 return None
1521
16- array = [1 ,2 ,3 ,3 ,3 ,3 ,4 ,4 ,4 ,4 ,5 ,6 ]
17- print (array )
18- print ("-----SEARCH-----" )
19- print ("found: " , 5 , " in index:" , binary_search (array , 5 ))
20- print ("-----SEARCH-----" )
21- print ("found: " , 6 , " in index:" , binary_search (array , 6 ))
22- print ("-----SEARCH-----" )
23- print ("found: " , 7 , " in index:" , binary_search (array , 7 ))
24- print ("-----SEARCH-----" )
25- print ("found: " , - 1 , " in index:" , binary_search (array , - 1 ))
26- print ("-----SEARCH-----" )
22+
23+ def main ():
24+ array = [1 , 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 4 , 5 , 6 ]
25+ print (array )
26+ print ("-----SEARCH-----" )
27+ print ("found: " , 5 , " in index:" , binary_search (array , 5 ))
28+ print ("-----SEARCH-----" )
29+ print ("found: " , 6 , " in index:" , binary_search (array , 6 ))
30+ print ("-----SEARCH-----" )
31+ print ("found: " , 7 , " in index:" , binary_search (array , 7 ))
32+ print ("-----SEARCH-----" )
33+ print ("found: " , - 1 , " in index:" , binary_search (array , - 1 ))
34+ print ("-----SEARCH-----" )
35+
36+ if __name__ == "__main__" :
37+ main ()
0 commit comments