|
| 1 | +## Binary Search |
| 2 | + |
| 3 | +#### Introduction |
| 4 | + |
| 5 | +- Binary search also known as **Half-Interval Search** or **logarithmic search** . |
| 6 | +- Binary search is used to find the target value within a sorted array. |
| 7 | + |
| 8 | +#### Simple Steps: |
| 9 | +- Binary search compares the target value to the middle element of the array. |
| 10 | +- If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, |
| 11 | +- again taking the middle element to compare to the target value,and repeating this until the target value is found. |
| 12 | +- If the search ends with the remaining half being empty, the target is not in the array. |
| 13 | + |
| 14 | + |
| 15 | +#### Advantages |
| 16 | +- Binary search is faster than linear search except for small arrays. |
| 17 | +- The algorithm is faster compared to linear search. |
| 18 | + |
| 19 | + |
| 20 | +#### Disadvantages |
| 21 | +- array must be sorted first to be able to apply binary search. |
| 22 | +- binary search is Slower than hash tables. |
| 23 | +- The worst case and the best case are nearly same for the binary search |
| 24 | + |
| 25 | +#### Algorithm |
| 26 | +Consider Variables |
| 27 | +``` |
| 28 | +input Variable |
| 29 | +lst : Input List(sorted list) |
| 30 | +number: Search ELement |
| 31 | +
|
| 32 | +Output Variable |
| 33 | +result : True if Element is Found Else False |
| 34 | +middle(index) : -1 if element is not found else return index of search element. |
| 35 | +iteration : return Number of iteration |
| 36 | +``` |
| 37 | +**step 1** : set start variable to 0 and end variable to (length(lst)-1) |
| 38 | +```python |
| 39 | +start=0 |
| 40 | +end=length(lst)-1 |
| 41 | +``` |
| 42 | + |
| 43 | +**step 2** : if start is greater than end break the algorithm |
| 44 | + |
| 45 | +**step 3** : calculate middle variable |
| 46 | +```python |
| 47 | +middle=(start+end)//2 |
| 48 | +``` |
| 49 | + |
| 50 | +**step 4** : Check Following Conditions: |
| 51 | + |
| 52 | +1. number==lst[middle] |
| 53 | + - If the targe number is equal to list[middle] then search is complete . |
| 54 | + - Break the loop |
| 55 | + |
| 56 | +2. number>lst[middle] |
| 57 | + - set the start to middle+1 |
| 58 | + ```python |
| 59 | + start=middle+1 |
| 60 | + ``` |
| 61 | + - Got to Step 2 and repeat |
| 62 | + |
| 63 | +3. number<lst[middle] |
| 64 | + - set the end to middle-1 |
| 65 | + ```python |
| 66 | + end=middle-1 |
| 67 | + ``` |
| 68 | + - Got to Step 2 and repeat |
| 69 | + |
| 70 | +#### Program: |
| 71 | +```python |
| 72 | +def BinarySearch(lst,number): |
| 73 | + '''Function Input |
| 74 | + lst: A Integer Element List |
| 75 | + number: The Number which Do you Want to Search |
| 76 | + |
| 77 | + Function Output: |
| 78 | + result: True if number is found else False |
| 79 | + middle(index): if element found then index else return -1 if element is not found |
| 80 | + iteration : Total No of Iteration Required |
| 81 | + ''' |
| 82 | + |
| 83 | + result=False |
| 84 | + iteration=0 |
| 85 | + length=len(lst) |
| 86 | + |
| 87 | + start=0 |
| 88 | + end=length-1 |
| 89 | + |
| 90 | + while start<=end: |
| 91 | + iteration+=1 |
| 92 | + middle=(start+end)//2 |
| 93 | + if lst[middle]==number: |
| 94 | + result=True |
| 95 | + break |
| 96 | + |
| 97 | + elif number>lst[middle]: |
| 98 | + start=middle+1 |
| 99 | + elif number <lst[middle]: |
| 100 | + end=middle-1 |
| 101 | + else: |
| 102 | + middle=-1 |
| 103 | + |
| 104 | + return result,middle,iteration |
| 105 | + |
| 106 | +#Driver Program |
| 107 | +#Simple List |
| 108 | +lst=[1,2,3,4,5,6,7,8,9,10] |
| 109 | + |
| 110 | +# Print Normal List |
| 111 | +print(lst) |
| 112 | +#Get Use Input From User |
| 113 | +number=int(input("Enter Search Number:")) |
| 114 | + |
| 115 | +#Call Search Function and save result in res and No of iteration in iteration |
| 116 | +res,index,iteration=BinarySearch(lst,number) |
| 117 | + |
| 118 | +#Print the Result |
| 119 | +print("\nNumber In List: {} \nNumber Index :{} \nRequired iteration: {}".format(res,index,iteration)) |
| 120 | + |
| 121 | +``` |
| 122 | + |
| 123 | +Output for 2 Input |
| 124 | +```python |
| 125 | +[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 126 | +Enter Search Number: 2 |
| 127 | + |
| 128 | +Number In List: True |
| 129 | +Number Index :1 |
| 130 | +Required iteration: 2 |
| 131 | +``` |
| 132 | + |
| 133 | +Output For 11 Input |
| 134 | +```python |
| 135 | +[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 136 | +Enter Search Number: 11 |
| 137 | + |
| 138 | +Number In List: False |
| 139 | +Number Index :-1 |
| 140 | +Required iteration: 4 |
| 141 | +``` |
| 142 | + |
| 143 | +Output for 5 input |
| 144 | +```python |
| 145 | +[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
| 146 | +Enter Search Number: 5 |
| 147 | + |
| 148 | +Number In List: True |
| 149 | +Number Index :4 |
| 150 | +Required iteration: 1 |
| 151 | +``` |
| 152 | + |
| 153 | +#### Time Complexity |
| 154 | +- Best Case O(1) |
| 155 | +- Average Case O(log n) |
| 156 | +- Worst Case O(log n) |
| 157 | + |
| 158 | +#### Space Complexity |
| 159 | +O(1) |
0 commit comments