-
Notifications
You must be signed in to change notification settings - Fork 21.2k
Expand file tree
/
Copy pathSentinelBinarySearch.java
More file actions
47 lines (39 loc) · 1.08 KB
/
SentinelBinarySearch.java
File metadata and controls
47 lines (39 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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(log n)
* Best case: O(1)
*
* <a href="https://en.wikipedia.org/wiki/Linear_search#With_a_sentinel">Wiki</a>
*/
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 < n - 1 || last == target) {
return i;
}
return -1;
}
}