|
| 1 | +# Searching Algorithms |
| 2 | + |
| 3 | +Searching algorithms are techniques used to locate specific items within a collection of data. These algorithms are fundamental in computer science and are employed in various applications, from databases to web search engines. |
| 4 | + |
| 5 | +## Real Life Example of Searching |
| 6 | +- Searching for a word in a dictionary |
| 7 | +- Searching for a specific book in a library |
| 8 | +- Searching for a contact in your phone's address book |
| 9 | +- Searching for a file on your computer, etc. |
| 10 | + |
| 11 | +# Some common searching techniques |
| 12 | + |
| 13 | +# 1. Linear Search |
| 14 | + |
| 15 | +Linear search, also known as sequential search, is a straightforward searching algorithm that checks each element in a collection until the target element is found or the entire collection has been traversed. It is simple to implement but becomes inefficient for large datasets. |
| 16 | + |
| 17 | +**Algorithm Overview:** |
| 18 | +- **Sequential Checking:** The algorithm iterates through each element in the collection, starting from the first element. |
| 19 | +- **Comparing Elements:** At each iteration, it compares the current element with the target element. |
| 20 | +- **Finding the Target:** If the current element matches the target, the search terminates, and the index of the element is returned. |
| 21 | +- **Completing the Search:** If the entire collection is traversed without finding the target, the algorithm indicates that the element is not present. |
| 22 | + |
| 23 | +## Linear Search Code in Python |
| 24 | + |
| 25 | +```python |
| 26 | +def linear_search(arr, target): |
| 27 | + for i in range(len(arr)): |
| 28 | + if arr[i] == target: |
| 29 | + return i |
| 30 | + return -1 |
| 31 | + |
| 32 | +arr = [5, 3, 8, 1, 2] |
| 33 | +target = 8 |
| 34 | +result = linear_search(arr, target) |
| 35 | +if result != -1: |
| 36 | + print(f"Element {target} found at index {result}.") |
| 37 | +else: |
| 38 | + print(f"Element {target} not found.") |
| 39 | +``` |
| 40 | + |
| 41 | +## Complexity Analysis |
| 42 | +- **Time Complexity**: O(n) |
| 43 | +- **Space Complexity**: O(1) |
| 44 | + |
| 45 | +</br> |
| 46 | +<hr> |
| 47 | +</br> |
| 48 | + |
| 49 | +# 2. Binary Search |
| 50 | + |
| 51 | +Binary search is an efficient searching algorithm that works on sorted collections. It repeatedly divides the search interval in half until the target element is found or the interval is empty. Binary search is significantly faster than linear search but requires the collection to be sorted beforehand. |
| 52 | + |
| 53 | +**Algorithm Overview:** |
| 54 | +- **Initial State:** Binary search starts with the entire collection as the search interval. |
| 55 | +- **Divide and Conquer:** At each step, it calculates the middle element of the current interval and compares it with the target. |
| 56 | +- **Narrowing Down the Interval:** If the middle element is equal to the target, the search terminates successfully. Otherwise, it discards half of the search interval based on the comparison result. |
| 57 | +- **Repeating the Process:** The algorithm repeats this process on the remaining half of the interval until the target is found or the interval is empty. |
| 58 | + |
| 59 | +## Binary Search Code in Python (Iterative) |
| 60 | + |
| 61 | +```python |
| 62 | +def binary_search(arr, target): |
| 63 | + low = 0 |
| 64 | + high = len(arr) - 1 |
| 65 | + while low <= high: |
| 66 | + mid = (low + high) // 2 |
| 67 | + if arr[mid] == target: |
| 68 | + return mid |
| 69 | + elif arr[mid] < target: |
| 70 | + low = mid + 1 |
| 71 | + else: |
| 72 | + high = mid - 1 |
| 73 | + return -1 |
| 74 | + |
| 75 | +arr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] |
| 76 | +target = 13 |
| 77 | +result = binary_search(arr, target) |
| 78 | +if result != -1: |
| 79 | + print(f"Element {target} found at index {result}.") |
| 80 | +else: |
| 81 | + print(f"Element {target} not found.") |
| 82 | +``` |
| 83 | + |
| 84 | +## Binary Search Code in Python (Recursive) |
| 85 | + |
| 86 | +```python |
| 87 | +def binary_search_recursive(arr, target, low, high): |
| 88 | + if low <= high: |
| 89 | + mid = (low + high) // 2 |
| 90 | + if arr[mid] == target: |
| 91 | + return mid |
| 92 | + elif arr[mid] < target: |
| 93 | + return binary_search_recursive(arr, target, mid + 1, high) |
| 94 | + else: |
| 95 | + return binary_search_recursive(arr, target, low, mid - 1) |
| 96 | + else: |
| 97 | + return -1 |
| 98 | + |
| 99 | +arr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] |
| 100 | +target = 13 |
| 101 | +result = binary_search_recursive(arr, target, 0, len(arr) - 1) |
| 102 | +if result != -1: |
| 103 | + print(f"Element {target} found at index {result}.") |
| 104 | +else: |
| 105 | + print(f"Element {target} not found.") |
| 106 | +``` |
| 107 | + |
| 108 | +## Complexity Analysis |
| 109 | +- **Time Complexity**: O(log n) |
| 110 | +- **Space Complexity**: O(1) (Iterative), O(log n) (Recursive) |
| 111 | + |
| 112 | +</br> |
| 113 | +<hr> |
| 114 | +</br> |
| 115 | + |
| 116 | +# 3. Interpolation Search |
| 117 | + |
| 118 | +Interpolation search is an improved version of binary search, especially useful when the elements in the collection are uniformly distributed. Instead of always dividing the search interval in half, interpolation search estimates the position of the target element based on its value and the values of the endpoints of the search interval. |
| 119 | + |
| 120 | +**Algorithm Overview:** |
| 121 | +- **Estimating Position:** Interpolation search calculates an approximate position of the target element within the search interval based on its value and the values of the endpoints. |
| 122 | +- **Refining the Estimate:** It adjusts the estimated position based on whether the target value is likely to be closer to the beginning or end of the search interval. |
| 123 | +- **Updating the Interval:** Using the refined estimate, it narrows down the search interval iteratively until the target is found or the interval becomes empty. |
| 124 | + |
| 125 | +## Interpolation Search Code in Python |
| 126 | + |
| 127 | +```python |
| 128 | +def interpolation_search(arr, target): |
| 129 | + low = 0 |
| 130 | + high = len(arr) - 1 |
| 131 | + while low <= high and arr[low] <= target <= arr[high]: |
| 132 | + if low == high: |
| 133 | + if arr[low] == target: |
| 134 | + return low |
| 135 | + return -1 |
| 136 | + pos = low + ((target - arr[low]) * (high - low)) // (arr[high] - arr[low]) |
| 137 | + if arr[pos] == target: |
| 138 | + return pos |
| 139 | + elif arr[pos] < target: |
| 140 | + low = pos + 1 |
| 141 | + else: |
| 142 | + high = pos - 1 |
| 143 | + return -1 |
| 144 | + |
| 145 | +arr = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] |
| 146 | +target = 60 |
| 147 | +result = interpolation_search(arr, target) |
| 148 | +if result != -1: |
| 149 | + print(f"Element {target} found at index {result}.") |
| 150 | +else: |
| 151 | + print(f"Element {target} not found.") |
| 152 | +``` |
| 153 | + |
| 154 | +## Complexity Analysis |
| 155 | +- **Time Complexity**: O(log log n) (Average) |
| 156 | +- **Space Complexity**: O(1) |
| 157 | + |
| 158 | +</br> |
| 159 | +<hr> |
| 160 | +</br> |
| 161 | + |
0 commit comments