-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Java] : Binary Search Iterative/Recursive approach and some coding s…
…tyle changes (#13) Co-authored-by: Ritabrata Nag <[email protected]>
- Loading branch information
1 parent
4552451
commit effa73e
Showing
6 changed files
with
84 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Created by Ritabrata, 31/08/2024 | ||
*/ | ||
|
||
package Codes_contest.binSearch; | ||
|
||
import java.util.Scanner; | ||
|
||
public class iterativeBinSearch { | ||
public static int binSearchIteration(int[] a, int n, int t) { | ||
int low = 0; | ||
int high = n - 1; | ||
int mid = -1; | ||
while (low <= high) { | ||
mid = (low + high) / 2; | ||
if (a[mid] == t) { | ||
return mid; | ||
} else if (t > a[mid]) { | ||
low = mid + 1; | ||
} else { | ||
high = mid - 1; | ||
} | ||
} | ||
return mid; | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
int n = sc.nextInt(); | ||
int[] a = new int[n]; | ||
for (int i = 0; i < n; i++) { | ||
a[i] = sc.nextInt(); | ||
} | ||
int target = sc.nextInt(); | ||
int index = binSearchIteration(a, n, target); | ||
System.out.println((index != -1) ? "Target is found" : "Target is not found"); | ||
System.out.println("Target element is found at index : " + index); | ||
sc.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Created by Ritabrata, 31/08/2024 | ||
*/ | ||
|
||
|
||
package Codes_contest.binSearch; | ||
|
||
import java.util.Scanner; | ||
|
||
public class recursiveBinSearch { | ||
public static int recursionOnArray(int[] a, int n, int target, int low, int high) { | ||
if (low > high) { | ||
return -1; | ||
} | ||
int mid = (low + high) / 2; | ||
if (a[mid] == target) { | ||
return mid; | ||
} else if (a[mid] > target) { | ||
return recursionOnArray(a, n, target, low, mid - 1); | ||
} else { | ||
return recursionOnArray(a, n, target, mid + 1, high); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
int n = sc.nextInt(); | ||
int[] a = new int[n]; | ||
for (int i = 0; i < n; i++) { | ||
a[i] = sc.nextInt(); | ||
} | ||
int target = sc.nextInt(); | ||
int targetIndex = recursionOnArray(a, n, target, 0, n - 1); | ||
System.out.println("Target element found at index : " + targetIndex); | ||
sc.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
leetcode/2751-robot-collisions.java → leetcode/RobotCollisions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters