-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathSortedSearchNoSize.java
64 lines (57 loc) · 1.63 KB
/
SortedSearchNoSize.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
package chapter10SortingAndSearching;
/**
*
* Problem: You are given an array-like data structure Listy which lacks a size
* method. It does however, have an elementAt(i) method that returns the element
* at index i in O(1) time. If i is beyond the bounds of the data structure, it
* return -1 (for this reason, the data structure only supports positive
* integers).Given a Listy which contains sorted, positive integers, find the
* index at which an element x occurs. If x occurs multiple times, you may
* return any index.
*
* Time Complexity: O(logN), find the length in O(logN), sort the length in
* O(logN)
*
*/
public class SortedSearchNoSize {
public static int search(Listy list, int val) {
int index = 1;
while (list.elementAt(index) != -1 && list.elementAt(index) < val) {
index *= 2;
}
return binarySearch(list, val, index / 2, index);
}
public static int binarySearch(Listy list, int val, int left, int right) {
int mid = 0;
while (left + 1 < right) {
mid = left + (right - left) / 2;
if (list.elementAt(mid) == val) {
return mid;
} else if (list.elementAt(mid) > val) {
// go left
right = mid;
} else {
// go right
left = mid;
}
}
return list.elementAt(left) == val ? left : list.elementAt(right) == val ? right : -1;
}
public static void main(String[] args) {
int[] array = { 1, 2, 3, 4, 5, 10, 15 };
Listy list = new Listy(array);
System.out.println(search(list, 2));
}
}
class Listy {
int[] array;
public Listy(int[] arr) {
array = arr.clone();
}
public int elementAt(int index) {
if (index >= array.length) {
return -1;
}
return array[index];
}
}