From de35e141774b9b11c2e34006660982e36737cf4d Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Sun, 1 Oct 2023 10:31:08 +0530 Subject: [PATCH 01/18] Create RecursiveBinarySearch.java --- .../searches/RecursiveBinarySearch.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java diff --git a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java new file mode 100644 index 000000000000..d75823ba0fc9 --- /dev/null +++ b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java @@ -0,0 +1,55 @@ +//Code by Pronay Debnath +//Created:- 1/10/2023 +//File Name should be RecursiveBinarySearch.java + +import java.util.*; +public class RecursiveBinarySearch { + + // Recursive binary search function + static int binsear(int a[], int left, int right, int target) { + if (right >= left) { + int mid = left + (right - left) / 2; + + // If the element is present at the middle itself + if (a[mid] == target) + return mid; + + // If the element is not in the middle but in the left or right subarray + if (a[mid] > target) + return binsear(a, left, mid - 1, target); + + return binsear(a, 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); + + // Added user inputs + + System.out.print("Enter the number of elements in the array: "); + int n = sc.nextInt(); + + int a[] = new int[n]; + + 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(); + + int res = 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); + + } +} From 24b09eadd0aa49f305bce4a39ef74b681e0e8a88 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Sun, 1 Oct 2023 10:39:58 +0530 Subject: [PATCH 02/18] Update RecursiveBinarySearch.java --- .../java/com/thealgorithms/searches/RecursiveBinarySearch.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java index d75823ba0fc9..f98ee6e735c5 100644 --- a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java @@ -53,3 +53,5 @@ public static void main(String[] args) { } } + +// Explanation:- https://www.tutorialspoint.com/java-program-for-binary-search-recursive From 3ddef78317db7f3cc4a7ec1bfeea34a967f6fa7a Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Sun, 1 Oct 2023 10:51:50 +0530 Subject: [PATCH 03/18] Update RecursiveBinarySearch.java --- .../searches/RecursiveBinarySearch.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java index f98ee6e735c5..6f233b26d450 100644 --- a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java @@ -1,9 +1,10 @@ -//Code by Pronay Debnath -//Created:- 1/10/2023 -//File Name should be RecursiveBinarySearch.java +// Code by Pronay Debnath +// Created:- 1/10/2023 +// File Name should be RecursiveBinarySearch.java import java.util.*; public class RecursiveBinarySearch { + // Recursive binary search function static int binsear(int a[], int left, int right, int target) { @@ -11,12 +12,10 @@ static int binsear(int a[], int left, int right, int target) { int mid = left + (right - left) / 2; // If the element is present at the middle itself - if (a[mid] == target) - return mid; + if (a[mid] == target) return mid; // If the element is not in the middle but in the left or right subarray - if (a[mid] > target) - return binsear(a, left, mid - 1, target); + if (a[mid] > target) return binsear(a, left, mid - 1, target); return binsear(a, mid + 1, right, target); } @@ -50,7 +49,6 @@ public static void main(String[] args) { System.out.println("Element not found in the array."); else System.out.println("Element found at index " + res); - } } From 2d397668df6fb77aa7cc989943e4d207023de7e5 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Sun, 1 Oct 2023 10:55:30 +0530 Subject: [PATCH 04/18] Update RecursiveBinarySearch.java --- .../com/thealgorithms/searches/RecursiveBinarySearch.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java index 6f233b26d450..3094fd265f76 100644 --- a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java @@ -5,7 +5,6 @@ import java.util.*; public class RecursiveBinarySearch { - // Recursive binary search function static int binsear(int a[], int left, int right, int target) { if (right >= left) { @@ -26,9 +25,8 @@ static int binsear(int a[], int left, int right, int target) { public static void main(String[] args) { Scanner sc = new Scanner(System.in); - - // Added user inputs + // Added user inputs System.out.print("Enter the number of elements in the array: "); int n = sc.nextInt(); From 0f536406723a3e872889a7409cc617b0529f061f Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Sun, 1 Oct 2023 10:58:30 +0530 Subject: [PATCH 05/18] Update RecursiveBinarySearch.java --- .../java/com/thealgorithms/searches/RecursiveBinarySearch.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java index 3094fd265f76..80fd7c6ac810 100644 --- a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java @@ -25,7 +25,6 @@ static int binsear(int a[], int left, int right, int target) { public static void main(String[] args) { Scanner sc = new Scanner(System.in); - // Added user inputs System.out.print("Enter the number of elements in the array: "); int n = sc.nextInt(); From 5c3c4dee9ef665063883b1d0f8cd8323f27b6e75 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Sun, 1 Oct 2023 11:51:17 +0530 Subject: [PATCH 06/18] Create ReverseArray.java --- .../thealgorithms/others/ReverseArray.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/main/java/com/thealgorithms/others/ReverseArray.java diff --git a/src/main/java/com/thealgorithms/others/ReverseArray.java b/src/main/java/com/thealgorithms/others/ReverseArray.java new file mode 100644 index 000000000000..579ca857682a --- /dev/null +++ b/src/main/java/com/thealgorithms/others/ReverseArray.java @@ -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); + } +} From 01c0851142e7b4322fc2c97da7b465382ef08b31 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Sun, 1 Oct 2023 11:57:44 +0530 Subject: [PATCH 07/18] Update RecursiveBinarySearch.java --- .../java/com/thealgorithms/searches/RecursiveBinarySearch.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java index 80fd7c6ac810..f68b9c2a4a38 100644 --- a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java @@ -1,6 +1,7 @@ // 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 { @@ -48,5 +49,3 @@ public static void main(String[] args) { System.out.println("Element found at index " + res); } } - -// Explanation:- https://www.tutorialspoint.com/java-program-for-binary-search-recursive From fc1d9d79bf3ee69f9d6835c33415608e62435be2 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Sun, 1 Oct 2023 12:06:11 +0530 Subject: [PATCH 08/18] Update RecursiveBinarySearch.java --- .../searches/RecursiveBinarySearch.java | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java index f68b9c2a4a38..5e1520f4bd81 100644 --- a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java @@ -4,20 +4,29 @@ // Explanation:- https://www.tutorialspoint.com/java-program-for-binary-search-recursive import java.util.*; -public class RecursiveBinarySearch { + +public class RecursiveBinarySearch> { // Recursive binary search function - static int binsear(int a[], int left, int right, int target) { + public int binsear(T[] arr, int left, int right, T target) { if (right >= left) { int mid = left + (right - left) / 2; - // If the element is present at the middle itself - if (a[mid] == target) return mid; + // 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 not in the middle but in the left or right subarray - if (a[mid] > target) return binsear(a, left, mid - 1, target); + // If the element is greater than the target, search in the left subarray + if (comparison > 0) { + return binsear(arr, left, mid - 1, target); + } - return binsear(a, mid + 1, right, target); + // Otherwise, search in the right subarray + return binsear(arr, mid + 1, right, target); } // Element is not present in the array @@ -26,11 +35,11 @@ static int binsear(int a[], int left, int right, int target) { public static void main(String[] args) { Scanner sc = new Scanner(System.in); - // Added user inputs + // User inputs System.out.print("Enter the number of elements in the array: "); int n = sc.nextInt(); - int a[] = new int[n]; + Integer[] a = new Integer[n]; // You can change the array type as needed System.out.println("Enter the elements in sorted order:"); @@ -41,7 +50,8 @@ public static void main(String[] args) { System.out.print("Enter the target element to search for: "); int t = sc.nextInt(); - int res = binsear(a, 0, n - 1, t); + RecursiveBinarySearch searcher = new RecursiveBinarySearch<>(); + int res = searcher.binsear(a, 0, n - 1, t); if (res == -1) System.out.println("Element not found in the array."); From 7426730367dacf306c505415bfd64cd7388f16b0 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Sun, 1 Oct 2023 12:10:50 +0530 Subject: [PATCH 09/18] Create RecursiveBinarySearchTest.java --- .../searches/RecursiveBinarySearchTest.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java diff --git a/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java b/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java new file mode 100644 index 000000000000..eb03f3f11588 --- /dev/null +++ b/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java @@ -0,0 +1,39 @@ +// 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 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); + } +} From b10c54b42750efe779574cd630d8b2c38cbab01c Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Sun, 1 Oct 2023 12:27:15 +0530 Subject: [PATCH 10/18] Update RecursiveBinarySearchTest.java --- .../com/thealgorithms/searches/RecursiveBinarySearchTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java b/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java index eb03f3f11588..82300092e32d 100644 --- a/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java +++ b/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java @@ -3,6 +3,7 @@ // Test file updated with JUnit tests import static org.junit.Assert.*; + import org.junit.Test; public class RecursiveBinarySearchTest { From 81b4fcf5e41e3dd6dc90d532cb5c7ba588847bcc Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Sun, 1 Oct 2023 12:36:26 +0530 Subject: [PATCH 11/18] Update RecursiveBinarySearchTest.java --- .../thealgorithms/searches/RecursiveBinarySearchTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java b/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java index 82300092e32d..ca51e066fd25 100644 --- a/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java +++ b/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java @@ -2,9 +2,8 @@ // Date:- 1/10/2023 // Test file updated with JUnit tests -import static org.junit.Assert.*; - -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; // Import the JUnit 5 Test annotation public class RecursiveBinarySearchTest { From 28a6a9a8488db4564f6136c582471f27a063ba5d Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Sun, 1 Oct 2023 12:38:08 +0530 Subject: [PATCH 12/18] Delete src/main/java/com/thealgorithms/others/ReverseArray.java --- .../thealgorithms/others/ReverseArray.java | 45 ------------------- 1 file changed, 45 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/others/ReverseArray.java diff --git a/src/main/java/com/thealgorithms/others/ReverseArray.java b/src/main/java/com/thealgorithms/others/ReverseArray.java deleted file mode 100644 index 579ca857682a..000000000000 --- a/src/main/java/com/thealgorithms/others/ReverseArray.java +++ /dev/null @@ -1,45 +0,0 @@ -// 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); - } -} From b1d94dd35b95e2919f96684939d1f3b7e6a1a621 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Sun, 1 Oct 2023 12:42:13 +0530 Subject: [PATCH 13/18] Update RecursiveBinarySearchTest.java --- .../com/thealgorithms/searches/RecursiveBinarySearchTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java b/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java index ca51e066fd25..027fe42b0070 100644 --- a/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java +++ b/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java @@ -3,6 +3,7 @@ // Test file updated with JUnit tests import static org.junit.jupiter.api.Assertions.*; + import org.junit.jupiter.api.Test; // Import the JUnit 5 Test annotation public class RecursiveBinarySearchTest { From 2f0ad2764c310897e41d5bac892769243e4199e6 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Sun, 1 Oct 2023 12:46:54 +0530 Subject: [PATCH 14/18] Update RecursiveBinarySearchTest.java --- .../searches/RecursiveBinarySearchTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java b/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java index 027fe42b0070..3b2cdead1c6b 100644 --- a/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java +++ b/src/test/java/com/thealgorithms/searches/RecursiveBinarySearchTest.java @@ -11,30 +11,30 @@ public class RecursiveBinarySearchTest { @Test public void testBinarySearch() { // Create an instance of GenericBinarySearch - GenericBinarySearch searcher = new GenericBinarySearch<>(); + RecursiveBinarySearch searcher = new RecursiveBinarySearch<>(); // 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); + int result1 = searcher.binsear(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); + int result2 = searcher.binsear(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); + int result3 = searcher.binsear(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); + int result4 = searcher.binsear(arr4, 0, arr4.length - 1, target4); assertEquals(4, result4); } } From a8e1c74816772d26422c2ae003824deb22ab811f Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Sun, 1 Oct 2023 12:56:27 +0530 Subject: [PATCH 15/18] Create ReverseArray.java --- .../thealgorithms/others/ReverseArray.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/main/java/com/thealgorithms/others/ReverseArray.java diff --git a/src/main/java/com/thealgorithms/others/ReverseArray.java b/src/main/java/com/thealgorithms/others/ReverseArray.java new file mode 100644 index 000000000000..2860a5eadaec --- /dev/null +++ b/src/main/java/com/thealgorithms/others/ReverseArray.java @@ -0,0 +1,44 @@ +// Created by Pronay Debnath +// Date:- 1/10/2023 +// Updated the whole code with documentations and comments wherever necessary + + +import java.util.*; + +public class Main { + /** + * Reverses the elements of an array in-place. + * + * @param arr The array to be reversed. + * @param start The starting index for the reversal. + * @param end The ending index for the reversal. + */ + public static void reverseArray(int[] arr, int start, int end) { + int temp; + while (start < end) { + temp = arr[start]; + arr[start] = arr[end]; + arr[end] = temp; + start++; + end--; + } + System.out.println("Reversed array:"); + for (int i = 0; i < arr.length; i++) { + System.out.println(arr[i]); + } + } + + public static void main(String args[]) { + Scanner sc = new Scanner(System.in); + System.out.println("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; + reverseArray(arr, start, end); + } +} From 180a4a6a2e662d6bc84860904238ad3bfef71491 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Sun, 1 Oct 2023 12:58:32 +0530 Subject: [PATCH 16/18] Delete src/main/java/com/thealgorithms/others/ReverseArray.java --- .../thealgorithms/others/ReverseArray.java | 44 ------------------- 1 file changed, 44 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/others/ReverseArray.java diff --git a/src/main/java/com/thealgorithms/others/ReverseArray.java b/src/main/java/com/thealgorithms/others/ReverseArray.java deleted file mode 100644 index 2860a5eadaec..000000000000 --- a/src/main/java/com/thealgorithms/others/ReverseArray.java +++ /dev/null @@ -1,44 +0,0 @@ -// Created by Pronay Debnath -// Date:- 1/10/2023 -// Updated the whole code with documentations and comments wherever necessary - - -import java.util.*; - -public class Main { - /** - * Reverses the elements of an array in-place. - * - * @param arr The array to be reversed. - * @param start The starting index for the reversal. - * @param end The ending index for the reversal. - */ - public static void reverseArray(int[] arr, int start, int end) { - int temp; - while (start < end) { - temp = arr[start]; - arr[start] = arr[end]; - arr[end] = temp; - start++; - end--; - } - System.out.println("Reversed array:"); - for (int i = 0; i < arr.length; i++) { - System.out.println(arr[i]); - } - } - - public static void main(String args[]) { - Scanner sc = new Scanner(System.in); - System.out.println("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; - reverseArray(arr, start, end); - } -} From 9443326288e6ab07179faf1a426e9b78a7554091 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Sun, 1 Oct 2023 13:11:59 +0530 Subject: [PATCH 17/18] Update RecursiveBinarySearchTest.java From 85da80f4b3c6eb361fabb74d087870281a836697 Mon Sep 17 00:00:00 2001 From: Pronay Debnath Date: Sun, 1 Oct 2023 20:12:20 +0530 Subject: [PATCH 18/18] Update RecursiveBinarySearch.java --- .../searches/RecursiveBinarySearch.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java index 5e1520f4bd81..8a02b30a9f0e 100644 --- a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java @@ -5,7 +5,20 @@ import java.util.*; -public class RecursiveBinarySearch> { +// Create a SearchAlgorithm class with a generic type +abstract class SearchAlgorithm> { + // Abstract find method to be implemented by subclasses + public abstract int find(T[] arr, T target); +} + +public class RecursiveBinarySearch> extends SearchAlgorithm { + + // Override the find method as required + @Override + public int find(T[] arr, T target) { + // Call the recursive binary search function + return binsear(arr, 0, arr.length - 1, target); + } // Recursive binary search function public int binsear(T[] arr, int left, int right, T target) { @@ -51,7 +64,7 @@ public static void main(String[] args) { int t = sc.nextInt(); RecursiveBinarySearch searcher = new RecursiveBinarySearch<>(); - int res = searcher.binsear(a, 0, n - 1, t); + int res = searcher.find(a, t); if (res == -1) System.out.println("Element not found in the array.");