-
Notifications
You must be signed in to change notification settings - Fork 7
/
ArraySearchDemo.java
76 lines (71 loc) · 2.48 KB
/
ArraySearchDemo.java
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Demonstrate the basic array searching algorithms.
* @author Ilkka Kokkarinen
*/
public class ArraySearchDemo {
/**
* Unoptimized linear search.
* @param a The array to search the value in.
* @param x The element to search for.
* @return The first index in which the element is found,
* or -1 if that element does not exist in the array.
*/
public int linearSearch(int[] a, int x) {
int i = 0;
while(i < a.length && a[i] != x) { i++; }
return i < a.length ? i : -1;
}
/**
* Sentinel search optimizes away one comparison per array element.
* @param a The array to search the value in.
* @param x The element to search for.
* @return The first index in which the element is found,
* or -1 if that element does not exist in the array.
*/
public int sentinelSearch(int[] a, int x) {
int last = a[a.length - 1];
a[a.length - 1] = x;
int i = 0;
while(a[i] != x) { i++; }
a[a.length - 1] = last;
return (i < a.length - 1 || last == x) ? i : -1;
}
/**
* Unrolled search to halve the number of bounds checks.
* @param a The array to search the value in.
* @param x The element to search for.
* @return The first index in which the element is found,
* or -1 if that element does not exist in the array.
*/
public int unrolledSearch(int[] a, int x) {
int i = 0;
if(a.length % 2 == 1) { // odd man out
if(a[i++] == x) { return 0; }
}
while(i < a.length) {
if(a[i++] == x) { return i - 1; }
if(a[i++] == x) { return i - 1; }
}
return -1;
}
/**
* The classic binary search for sorted arrays.
* @param a The array to search the value in.
* @param x The element to search for.
* @return The first index in which the element is found. If the element
* {@code x} is not found, returns the index where that element would go
* to keep array in sorted order. If {@code x} is larger than the current
* largest element of the array, returns {@code a.length} as special case.
*/
// Binary search for sorted arrays.
public int binarySearch(int[] a, int x) {
int i = 0, j = a.length - 1;
if(a[j] < x) { return a.length; } // special case
while(i < j) {
int mid = (i+j) / 2;
if(a[mid] < x) { i = mid + 1; }
else { j = mid; }
}
return i;
}
}