From fd9aa28536e9965d8d210c86c8c355cddb742a5d Mon Sep 17 00:00:00 2001 From: Kstrella <90272570+Kstrella@users.noreply.github.com> Date: Sat, 30 Sep 2023 16:58:29 -0400 Subject: [PATCH 1/4] Added an Implementation for a Recursive Binary Search --- .../searches/RecursiveBinarySearch.java | 50 +++++++++++++++++++ 1 file changed, 50 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..834519c10b6e --- /dev/null +++ b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java @@ -0,0 +1,50 @@ +package com.thealgorithms.searches; + +public class RecursiveBinarySearch { + /** + * Binary search is a popular and useful algorithm used when searching in for a value in a sorted arrays. + * The algorithm finds the index of a target value within the array + * + *
+ * Best Case: O(1) + * Average Case: O(log N) + * Worst Case: O(log N) + * Space Complexity Iterative Approach: O(1) + * Space Complexity Recursive Approach: O(logN) + * + * @author Kevin Estrella (https://github.com/Kstrella) + */ + + public static int recBinarySearch(int[] arr, int start, int end, int target) + { + if (end >= start) { + // Find the middle of the array + int mid = start + (end - start) / 2; + + // If the target is the middle of the array return it + if (arr[mid] == target) + return mid; + + // If target is smaller than mid, then we search the left sub-array + if (arr[mid] > target) + return recBinarySearch(arr, start, mid - 1, target); + + // If target is greater than mid, then we search the right sub-array + return recBinarySearch(arr, mid + 1, end, target); + } + + //If the target does not exist in the array + return -1; + } + + public static void main(String args[]) + { + int arr[] = {1, 2, 4, 7, 10, 22, 31, 40,55,97,100}; + int target = 13; + int result = recBinarySearch(arr, 0, arr.length - 1, target); + if (result == -1) + System.out.println("Target element is not in the array"); + else + System.out.println("Target element is at index " + result); + } +} From d73583eaf9baaebfbb9031b9808fa98132533cfe Mon Sep 17 00:00:00 2001 From: Kstrella <90272570+Kstrella@users.noreply.github.com> Date: Sat, 30 Sep 2023 17:04:19 -0400 Subject: [PATCH 2/4] Added an Implementation for a Recursive Binary Search --- .../com/thealgorithms/searches/RecursiveBinarySearch.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java index 834519c10b6e..8cc8df65c9ee 100644 --- a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java @@ -37,9 +37,9 @@ public static int recBinarySearch(int[] arr, int start, int end, int target) return -1; } - public static void main(String args[]) + public static void main(String[] args) { - int arr[] = {1, 2, 4, 7, 10, 22, 31, 40,55,97,100}; + int[] arr = {1, 2, 4, 7, 10, 22, 31, 40,55,97,100}; int target = 13; int result = recBinarySearch(arr, 0, arr.length - 1, target); if (result == -1) From 63ce38d380450c2c5f79c1dfb52ce7cafdcbae6e Mon Sep 17 00:00:00 2001 From: Kstrella <90272570+Kstrella@users.noreply.github.com> Date: Sat, 30 Sep 2023 17:05:37 -0400 Subject: [PATCH 3/4] Added an Implementation for a Recursive Binary Search --- .../java/com/thealgorithms/searches/RecursiveBinarySearch.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java index 8cc8df65c9ee..3053f04da2e4 100644 --- a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java @@ -43,7 +43,7 @@ public static void main(String[] args) int target = 13; int result = recBinarySearch(arr, 0, arr.length - 1, target); if (result == -1) - System.out.println("Target element is not in the array"); + System.out.println("Target element is not in the array."); else System.out.println("Target element is at index " + result); } From c489b1c0dd67f386d8e889c7437ce9912d5e026f Mon Sep 17 00:00:00 2001 From: Kstrella <90272570+Kstrella@users.noreply.github.com> Date: Sat, 30 Sep 2023 17:22:52 -0400 Subject: [PATCH 4/4] Added an Implementation for a Recursive Binary Search --- .../searches/RecursiveBinarySearch.java | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java index 3053f04da2e4..ef967a2c3c9f 100644 --- a/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/RecursiveBinarySearch.java @@ -15,31 +15,25 @@ public class RecursiveBinarySearch { * @author Kevin Estrella (https://github.com/Kstrella) */ - public static int recBinarySearch(int[] arr, int start, int end, int target) - { + public static int recBinarySearch(int[] arr, int start, int end, int target) { if (end >= start) { // Find the middle of the array int mid = start + (end - start) / 2; // If the target is the middle of the array return it - if (arr[mid] == target) - return mid; + if (arr[mid] == target) return mid; // If target is smaller than mid, then we search the left sub-array - if (arr[mid] > target) - return recBinarySearch(arr, start, mid - 1, target); + if (arr[mid] > target) return recBinarySearch(arr, start, mid - 1, target); // If target is greater than mid, then we search the right sub-array return recBinarySearch(arr, mid + 1, end, target); } - - //If the target does not exist in the array return -1; } - public static void main(String[] args) - { - int[] arr = {1, 2, 4, 7, 10, 22, 31, 40,55,97,100}; + public static void main(String[] args) { + int[] arr = {1, 2, 4, 7, 10, 22, 31, 40, 55, 97, 100}; int target = 13; int result = recBinarySearch(arr, 0, arr.length - 1, target); if (result == -1)