-
Notifications
You must be signed in to change notification settings - Fork 20k
Added [FEATURE REQUEST] <Recursive Binary Search> #4457 #4469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
de35e14
Create RecursiveBinarySearch.java
debnath003 24b09ea
Update RecursiveBinarySearch.java
debnath003 3ddef78
Update RecursiveBinarySearch.java
debnath003 2d39766
Update RecursiveBinarySearch.java
debnath003 0f53640
Update RecursiveBinarySearch.java
debnath003 5c3c4de
Create ReverseArray.java
debnath003 01c0851
Update RecursiveBinarySearch.java
debnath003 fc1d9d7
Update RecursiveBinarySearch.java
debnath003 7426730
Create RecursiveBinarySearchTest.java
debnath003 b10c54b
Update RecursiveBinarySearchTest.java
debnath003 81b4fcf
Update RecursiveBinarySearchTest.java
debnath003 28a6a9a
Delete src/main/java/com/thealgorithms/others/ReverseArray.java
debnath003 b1d94dd
Update RecursiveBinarySearchTest.java
debnath003 2f0ad27
Update RecursiveBinarySearchTest.java
debnath003 a8e1c74
Create ReverseArray.java
debnath003 180a4a6
Delete src/main/java/com/thealgorithms/others/ReverseArray.java
debnath003 9443326
Update RecursiveBinarySearchTest.java
debnath003 85da80f
Update RecursiveBinarySearch.java
debnath003 a859027
Merge branch 'master' into master
debasishbsws File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,45 @@ | ||
// Created by Pronay Debnath | ||
// Date:- 1/10/2023 | ||
// Updated the existing code with documentations and some comments | ||
|
||
import java.util.*; | ||
|
||
public class ReverseArray { | ||
/** | ||
* Reverses the elements of an array in-place. | ||
* | ||
* @param a The array to be reversed. | ||
* @param start The starting index for the reversal. | ||
* @param end The ending index for the reversal. | ||
*/ | ||
public static void revarr(int[] a, int start, int end) { | ||
int temp; | ||
while (start < end) { | ||
temp = a[start]; | ||
a[start] = a[end]; | ||
a[end] = temp; | ||
start++; | ||
end--; | ||
} | ||
System.out.println("Reversed array:"); | ||
for (int i = 0; i < a.length; i++) { | ||
System.out.print(a[i]); | ||
System.out.println(); | ||
} | ||
} | ||
|
||
public static void main(String args[]) { | ||
Scanner sc = new Scanner(System.in); | ||
// Adding user input functionality | ||
System.out.print("Enter the array size: "); | ||
int n = sc.nextInt(); | ||
int[] arr = new int[n]; | ||
System.out.println("Enter array elements: "); | ||
for (int i = 0; i < n; i++) { | ||
arr[i] = sc.nextInt(); | ||
} | ||
int start = 0; | ||
int end = n - 1; | ||
revarr(arr, start, end); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java
This file contains hidden or 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,61 @@ | ||
// Code by Pronay Debnath | ||
// Created:- 1/10/2023 | ||
// File Name should be RecursiveBinarySearch.java | ||
// Explanation:- https://www.tutorialspoint.com/java-program-for-binary-search-recursive | ||
|
||
import java.util.*; | ||
|
||
public class RecursiveBinarySearch<T extends Comparable<T>> { | ||
|
||
// Recursive binary search function | ||
public int binsear(T[] arr, int left, int right, T target) { | ||
if (right >= left) { | ||
int mid = left + (right - left) / 2; | ||
|
||
// Compare the element at the middle with the target | ||
int comparison = arr[mid].compareTo(target); | ||
|
||
// If the element is equal to the target, return its index | ||
if (comparison == 0) { | ||
return mid; | ||
} | ||
|
||
// If the element is greater than the target, search in the left subarray | ||
if (comparison > 0) { | ||
return binsear(arr, left, mid - 1, target); | ||
} | ||
|
||
// Otherwise, search in the right subarray | ||
return binsear(arr, mid + 1, right, target); | ||
} | ||
|
||
// Element is not present in the array | ||
return -1; | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
// User inputs | ||
System.out.print("Enter the number of elements in the array: "); | ||
int n = sc.nextInt(); | ||
|
||
Integer[] a = new Integer[n]; // You can change the array type as needed | ||
|
||
System.out.println("Enter the elements in sorted order:"); | ||
|
||
for (int i = 0; i < n; i++) { | ||
a[i] = sc.nextInt(); | ||
} | ||
|
||
System.out.print("Enter the target element to search for: "); | ||
int t = sc.nextInt(); | ||
|
||
RecursiveBinarySearch<Integer> searcher = new RecursiveBinarySearch<>(); | ||
int res = searcher.binsear(a, 0, n - 1, t); | ||
|
||
if (res == -1) | ||
System.out.println("Element not found in the array."); | ||
else | ||
System.out.println("Element found at index " + res); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java
This file contains hidden or 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 Pronay Debnath | ||
// Date:- 1/10/2023 | ||
// Test file updated with JUnit tests | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Test; | ||
|
||
public class RecursiveBinarySearchTest { | ||
|
||
@Test | ||
public void testBinarySearch() { | ||
// Create an instance of GenericBinarySearch | ||
GenericBinarySearch<Integer> searcher = new GenericBinarySearch<>(); | ||
|
||
// Test case 1: Element found in the array | ||
Integer[] arr1 = {1, 2, 3, 4, 5}; | ||
int target1 = 3; | ||
int result1 = searcher.binarySearch(arr1, 0, arr1.length - 1, target1); | ||
assertEquals(2, result1); | ||
|
||
// Test case 2: Element not found in the array | ||
Integer[] arr2 = {1, 2, 3, 4, 5}; | ||
int target2 = 6; | ||
int result2 = searcher.binarySearch(arr2, 0, arr2.length - 1, target2); | ||
assertEquals(-1, result2); | ||
|
||
// Test case 3: Element found at the beginning of the array | ||
Integer[] arr3 = {10, 20, 30, 40, 50}; | ||
int target3 = 10; | ||
int result3 = searcher.binarySearch(arr3, 0, arr3.length - 1, target3); | ||
assertEquals(0, result3); | ||
|
||
// Test case 4: Element found at the end of the array | ||
Integer[] arr4 = {10, 20, 30, 40, 50}; | ||
int target4 = 50; | ||
int result4 = searcher.binarySearch(arr4, 0, arr4.length - 1, target4); | ||
assertEquals(4, result4); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.