|
3 | 3 |
|
4 | 4 | --- |
5 | 5 |
|
6 | | -# Linear Vs Binary - 🔍 Search |
| 6 | +# Linear Vs Binary - 🔍 Search - [Code](A_Search.java) |
7 | 7 |
|
8 | 8 | ```java |
9 | 9 | package com.akashdipmahapatra.DSA; |
@@ -91,10 +91,102 @@ public static int BinarySearch(int[] arr, int target){ |
91 | 91 | } |
92 | 92 | ``` |
93 | 93 |
|
94 | | -## Linear Vs Binary - 🔍 Search |
| 94 | +## Binary Search 🔍 O(log n) |
95 | 95 |
|
96 | 96 | <img src="../img/JAVA%20Search%20(1).png"> |
97 | 97 | <img src="../img/JAVA%20Search%20(2).png"> |
98 | 98 | <img src="../img/JAVA%20Search%20(3).png"> |
99 | 99 | <img src="../img/JAVA%20Search%20(4).png"> |
100 | 100 | <img src="../img/JAVA%20Search%20(5).png"> |
| 101 | + |
| 102 | +<img src="../img/line.gif"> |
| 103 | + |
| 104 | +# Iterative Vs Recursive |
| 105 | + |
| 106 | +> **No**, the binary search code I wrote and shared above is **not recursive** — it is **iterative**. |
| 107 | +
|
| 108 | +--- |
| 109 | + |
| 110 | +### 🔁 **Iterative Binary Search** |
| 111 | + |
| 112 | +✅ Uses a `while` loop </br> |
| 113 | +✅ Keeps updating `left` and `right` </br> |
| 114 | +❌ Does **not** call itself (no recursion) |
| 115 | + |
| 116 | +Previous code: |
| 117 | + |
| 118 | +```java |
| 119 | +while (left <= right) { |
| 120 | + int mid = (left + right) / 2; |
| 121 | + if (arr[mid] == target) return mid; |
| 122 | + else if (arr[mid] < target) left = mid + 1; |
| 123 | + else right = mid - 1; |
| 124 | +} |
| 125 | +``` |
| 126 | +--- |
| 127 | + |
| 128 | +### 🔁❌ Not Recursive Because: |
| 129 | + |
| 130 | +* It does **not** do: `return BinarySearch(arr, newLeft, newRight, target)` |
| 131 | +* It only loops — it doesn’t create a **stack of calls** |
| 132 | + |
| 133 | +--- |
| 134 | + |
| 135 | +## ❓ Which is Best ? |
| 136 | + |
| 137 | +> Let's compare **Iterative vs Recursive Binary Search** in terms of **Data Structures and Algorithms (DSA)** — especially for **time complexity**, **space**, and **interview preference**. |
| 138 | +
|
| 139 | +--- |
| 140 | + |
| 141 | +## ✅ 1. **Time Complexity** – Same for Both |
| 142 | + |
| 143 | +| Type | Best | Average | Worst | |
| 144 | +| ----------------------- | ---- | -------- | -------- | |
| 145 | +| Iterative Binary Search | O(1) | O(log n) | O(log n) | |
| 146 | +| Recursive Binary Search | O(1) | O(log n) | O(log n) | |
| 147 | + |
| 148 | +### 💡 Why O(log n)? |
| 149 | + |
| 150 | +Because in every step, the search space is divided by 2. |
| 151 | + |
| 152 | +--- |
| 153 | + |
| 154 | +## 🧠 2. **Space Complexity** – Different! |
| 155 | + |
| 156 | +| Type | Space Complexity | |
| 157 | +| ------------- | --------------------------------------------- | |
| 158 | +| **Iterative** | **O(1)** – Constant space (no extra memory) ✅ | |
| 159 | +| **Recursive** | **O(log n)** – Due to recursion call stack ❌ | |
| 160 | + |
| 161 | +### 🔥 Key Point: |
| 162 | + |
| 163 | +Recursive Binary Search uses extra memory for **function call stack** (because each recursive call is stored until it's resolved). Iterative doesn’t. |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +## 🧪 3. **In Practice / Interviews / Exams:** |
| 168 | + |
| 169 | +| Criteria | Recommended | |
| 170 | +| --------------------- | --------------------- | |
| 171 | +| **Performance** | Iterative ✅ | |
| 172 | +| **Memory Efficient** | Iterative ✅ | |
| 173 | +| **Shorter code** | Recursive (sometimes) | |
| 174 | +| **TCS DCA / NQT** | Iterative ✅ | |
| 175 | +| **Coding Interviews** | Iterative ✅ | |
| 176 | + |
| 177 | +--- |
| 178 | + |
| 179 | +## 🧾 Final Verdict: |
| 180 | + |
| 181 | +| Factor | Winner | |
| 182 | +| ------------------- | ----------------------- | |
| 183 | +| Time Complexity | ✅ Tie (Same) | |
| 184 | +| Space Efficiency | ✅ Iterative | |
| 185 | +| Simplicity in logic | ❌ Recursive (but risky) | |
| 186 | +| Industry/Exam use | ✅ Iterative | |
| 187 | + |
| 188 | +--- |
| 189 | + |
| 190 | +### ✅ So, **Iterative Binary Search is better overall** for DSA practice, exams like **TCS NQT/DCA**, and coding interviews. |
| 191 | + |
| 192 | + |
0 commit comments