-
Notifications
You must be signed in to change notification settings - Fork 321
Expand file tree
/
Copy pathq04.js
More file actions
51 lines (44 loc) · 1.41 KB
/
q04.js
File metadata and controls
51 lines (44 loc) · 1.41 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
/*10.4 Sorted Search, No Size: 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 0(1) time. If i is beyond the bounds of the
data structure, it returns -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.*/
export function Listy(array) {
this._array = array;
}
Listy.prototype.elementAt = function (index) {
if (index >= this._array.length) {
return -1;
}
return this._array[index];
};
export function findIndex(listy, x) {
//looking for borders of binary search
let left = 0;
let right = 3 * left + 1;
while (listy.elementAt(right) !== -1 && listy.elementAt(right) < x) {
left = right;
right = 3 * left + 1;
}
//doing search
while (right !== left) {
if (x === listy.elementAt(right)) {
return right;
}
if (x === listy.elementAt(left)) {
return left;
}
if (-1 === listy.elementAt(left) && -1 === listy.elementAt(right)) {
return -1;
}
const middle = Math.floor(left + (right - left) / 2);
if (x <= listy.elementAt(middle) || middle === left || -1 === listy.elementAt(middle)) {
right = middle;
}
else {
left = middle;
}
}
return -1;
}