-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLesson 18.java
51 lines (46 loc) · 1.17 KB
/
Lesson 18.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
// Student: Griffin Gowdey.
// Instructor: Daniel Goodman.
// Class Number: ITSE1479-86039.
// Class Name: Java Programming.
// Semester: Fall 2020.
// Lesson 18.
/*
Assignment:
Implement a generic version of the binary search algorithm.
*/
public class Lesson18
{
public class BinarySearcher<T extends Comparable>
{
private T[] a;
/* Constructs a BinarySearcher.
@param anArray a sorted array. */
public BinarySearcher(T[] anArray)
{
a = anArray;
}
/*
Finds a value in a sorted array, using the binary search algorithm.
@param v the value to search.
@return the index at which the value occurs, or 1 if it does not
occur in the array.
*/
public int search(T v)
{
int low = 0;
int high = a.length - 1;
while (low <= high)
{
int mid = (low + high) / 2;
int diff = a[mid].compareTo(v);
if (diff == 0)
return mid;
else if (diff < 0)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
}
}