Skip to content

Commit 3f4086c

Browse files
committed
Recursive
1 parent f48ea3c commit 3f4086c

5 files changed

Lines changed: 145 additions & 7 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.akashdipmahapatra.DSA;
22

3-
public class A_Search {
3+
public class A01_Search {
44

55
public static void main(String[] args){
66

DSA/A02_Recursive.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.akashdipmahapatra.DSA;
2+
3+
public class A02_Recursive {
4+
5+
public static void main(String[] args){
6+
7+
// int arr[] = new int[10000];
8+
// for(int i=0;i< arr.length;i++){
9+
// arr[i]=i+1; // Fills 1 to 100
10+
// }
11+
//
12+
// int target = 900;
13+
14+
int arr[] = {5, 7, 9, 11, 13, 15};
15+
int target = 15;
16+
17+
// Method
18+
int result = BinarySearch(arr, target, 0, arr.length-1);
19+
20+
// output
21+
if(result != -1) {
22+
System.out.println("Element found at Index: " + result);
23+
}else{
24+
System.out.println("Element not found");
25+
}
26+
}
27+
28+
29+
30+
31+
public static int BinarySearch(int[] arr, int target, int left, int right) {
32+
// 5, 7, 9, 11, 13, 15
33+
34+
35+
if(left<=right){
36+
int mid = (left + right)/2;
37+
38+
if(arr[mid] == target){
39+
return mid;
40+
}else if(arr[mid] < target){
41+
return BinarySearch(arr, target, mid+1, right);
42+
}else{
43+
return BinarySearch(arr, target, left, mid-1);
44+
}
45+
}
46+
47+
return -1;
48+
}
49+
}
50+

DSA/B.java

Lines changed: 0 additions & 4 deletions
This file was deleted.

DSA/README.md

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
---
55

6-
# Linear Vs Binary - 🔍 Search
6+
# Linear Vs Binary - 🔍 Search - [Code](A_Search.java)
77

88
```java
99
package com.akashdipmahapatra.DSA;
@@ -91,10 +91,102 @@ public static int BinarySearch(int[] arr, int target){
9191
}
9292
```
9393

94-
## Linear Vs Binary - 🔍 Search
94+
## Binary Search 🔍 O(log n)
9595

9696
<img src="../img/JAVA%20Search%20(1).png">
9797
<img src="../img/JAVA%20Search%20(2).png">
9898
<img src="../img/JAVA%20Search%20(3).png">
9999
<img src="../img/JAVA%20Search%20(4).png">
100100
<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+

img/line.gif

3.9 KB
Loading

0 commit comments

Comments
 (0)