From 390dd43655efc0f968ac84cba74c7bdbf900a5f1 Mon Sep 17 00:00:00 2001 From: Pranjali86 Date: Thu, 7 May 2026 18:59:53 +0530 Subject: [PATCH 1/4] Add: SentinelBinarySearch algorithm with tests --- .../searches/SentinelBinarySearch.java | 44 +++++++++++++++++++ .../searches/SentinelBinarySearchTest.java | 38 ++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 src/main/java/com/thealgorithms/searches/SentinelBinarySearch.java create mode 100644 src/test/java/com/thealgorithms/searches/SentinelBinarySearchTest.java diff --git a/src/main/java/com/thealgorithms/searches/SentinelBinarySearch.java b/src/main/java/com/thealgorithms/searches/SentinelBinarySearch.java new file mode 100644 index 000000000000..e73d296d88bf --- /dev/null +++ b/src/main/java/com/thealgorithms/searches/SentinelBinarySearch.java @@ -0,0 +1,44 @@ +package com.thealgorithms.searches; + +/** +* Sentinel Binary Search algorithm implementation +* +* Sentinel Binary Search is a variation of Binary Search that +* reduces the number of comparisons by placing a sentinel value +* at the end of the array, eliminating the need to check array +* bounds on every step. +* +* Worst case: O(logn) +* Best case: O(1) +*/ +public class SentinelBinarySearch { + /** + * Finds the index of a target value in a sorted array. + * + * @param arr the sorted array to search + * @param target the value to find + * @return the index of target if found, otherwise -1 + */ + public int find(int[] arr, int target){ + int n = arr.length; + + if(n == 0){ + return -1; + } + + int last = arr[n-1]; + arr[n-1] = target; + + int i = 0; + while(arr[i] != target){ + i++; + } + + arr[n-1] = last; + + if (i Date: Thu, 7 May 2026 19:12:36 +0530 Subject: [PATCH 2/4] Add: Wikipedia reference link to SentinelBinarySearch --- .../java/com/thealgorithms/searches/SentinelBinarySearch.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/thealgorithms/searches/SentinelBinarySearch.java b/src/main/java/com/thealgorithms/searches/SentinelBinarySearch.java index e73d296d88bf..c89934475cad 100644 --- a/src/main/java/com/thealgorithms/searches/SentinelBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/SentinelBinarySearch.java @@ -10,6 +10,8 @@ * * Worst case: O(logn) * Best case: O(1) +* +* Wiki */ public class SentinelBinarySearch { /** From 966e11fcaf192b3a90f40e01e275fed6e73c7e0e Mon Sep 17 00:00:00 2001 From: Pranjali86 Date: Sun, 24 May 2026 19:13:02 +0530 Subject: [PATCH 3/4] Fix: formatting issues in SentinelBinarySearch --- .../searches/SentinelBinarySearch.java | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/SentinelBinarySearch.java b/src/main/java/com/thealgorithms/searches/SentinelBinarySearch.java index c89934475cad..bdbf229cd8be 100644 --- a/src/main/java/com/thealgorithms/searches/SentinelBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/SentinelBinarySearch.java @@ -1,46 +1,47 @@ package com.thealgorithms.searches; /** -* Sentinel Binary Search algorithm implementation -* -* Sentinel Binary Search is a variation of Binary Search that -* reduces the number of comparisons by placing a sentinel value -* at the end of the array, eliminating the need to check array -* bounds on every step. -* -* Worst case: O(logn) -* Best case: O(1) -* -* Wiki -*/ + * Sentinel Binary Search algorithm implementation. + * + * Sentinel Binary Search is a variation of Binary Search that reduces + * the number of comparisons by placing a sentinel value at the end + * of the array, eliminating the need to check array bounds on every step. + * + * Worst case: O(log n) + * Best case: O(1) + * + * Wiki + */ public class SentinelBinarySearch { + /** * Finds the index of a target value in a sorted array. - * + * * @param arr the sorted array to search - * @param target the value to find + * @param target the value to find * @return the index of target if found, otherwise -1 */ - public int find(int[] arr, int target){ + public int find(int[] arr, int target) { int n = arr.length; - if(n == 0){ + if (n == 0) { return -1; } - int last = arr[n-1]; - arr[n-1] = target; + int last = arr[n - 1]; + arr[n - 1] = target; int i = 0; - while(arr[i] != target){ + while (arr[i] != target) { i++; } - arr[n-1] = last; + arr[n - 1] = last; - if (i Date: Mon, 25 May 2026 08:18:02 +0530 Subject: [PATCH 4/4] Fix: add newline at end of files and fix imports --- .../com/thealgorithms/searches/SentinelBinarySearch.java | 2 +- .../thealgorithms/searches/SentinelBinarySearchTest.java | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/searches/SentinelBinarySearch.java b/src/main/java/com/thealgorithms/searches/SentinelBinarySearch.java index bdbf229cd8be..c3f4147dbb65 100644 --- a/src/main/java/com/thealgorithms/searches/SentinelBinarySearch.java +++ b/src/main/java/com/thealgorithms/searches/SentinelBinarySearch.java @@ -44,4 +44,4 @@ public int find(int[] arr, int target) { return -1; } -} \ No newline at end of file +} diff --git a/src/test/java/com/thealgorithms/searches/SentinelBinarySearchTest.java b/src/test/java/com/thealgorithms/searches/SentinelBinarySearchTest.java index 37b3c8438765..fc6c658f8e6d 100644 --- a/src/test/java/com/thealgorithms/searches/SentinelBinarySearchTest.java +++ b/src/test/java/com/thealgorithms/searches/SentinelBinarySearchTest.java @@ -1,19 +1,20 @@ package com.thealgorithms.searches; +import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; -public class SentinelBinarySearchTest { +class SentinelBinarySearchTest { + private final SentinelBinarySearch search = new SentinelBinarySearch(); @Test - void testElementFound(){ + void testElementFound() { int[] arr = {1, 3, 5, 7, 9}; assertEquals(2, search.find(arr, 5)); } @Test - void testElementNotFound(){ + void testElementNotFound() { int[] arr = {1, 3, 5, 7, 9}; assertEquals(-1, search.find(arr, 4)); }