-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
81 lines (71 loc) · 2.18 KB
/
Copy pathBinarySearch.java
File metadata and controls
81 lines (71 loc) · 2.18 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
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
77
78
79
80
81
//Binary Search for List of Strings
//array of Strings have been sorted by first letter of each string
//Each string is unique
public class BinarySearch
{
//Iterative method
public static int binarySearchIterative(String[] A, String s)
{
//instantiate left and right bounds
int left = 0; int right = A.length-1;
while(left < right)
{
//get median index of array
int mid = (left + right) / 2;
//compare with string val s
if (s.charAt(0) < A[mid].charAt(0))
{
//move upperbound of slice to one less than mid
right = mid -1;
}
else if (s.charAt(0) > A[mid].charAt(0))
{
//move lowerbound of slice to one more than mid
left = mid +1;
}
else if(A[mid].charAt(0) == s.charAt(0))
{
return mid;
}
}
//if string value doesn't exist in list.
return -1;
}
public static int binarySearchIterative(int[] A, int x)
{
//instantiate left and right bounds
int left = 0; int right = A.length;
// int mid = 0;
while(left < right)
{
//get median indx of array
// int mid = (left + right) / 2;
int mid = left + (right - left)/2;
//compare x with val at mid index
if(x < A[mid])
{
right = mid -1;
}
else if(x > A[mid])
{
left = mid +1;
}
else if(x == A[mid])
{
return mid;
}
}
// System.out.println(A[mid]);
//if value is not found in array
return -1;
}
public static void main(String[] args)
{
// String s = "apple";
// String[] A = {"apple","oranges","guava","juice","pineapple"};
// System.out.println(binarySearchIterative(A, s ) );
int x = 4;
int[] N = {1,3,4,5,6,7,8,9};
System.out.println(binarySearchIterative(N, x));
}
}