-
Notifications
You must be signed in to change notification settings - Fork 0
/
findRotatedIndex.js
62 lines (60 loc) · 1.79 KB
/
findRotatedIndex.js
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
function BS(arr, num, start, end) {
if (arr[start] === num)
return start;
if (arr[end] === num)
return end;
if (end <= start)
return -1
var mid = start + Math.floor((end - start) / 2);
if (num === arr[mid])
return mid;
if (num > arr[mid])
return BS(arr, num, mid, end);
if (num < arr[mid])
return BS(arr, num, start, mid);
}
function BSRev(arr, num, start, end) {
if (arr[start] === num)
return start;
if (arr[end] === num)
return end;
if (end <= start)
return -1
var mid = start + Math.floor((end - start) / 2);
if (num === arr[mid])
return mid;
if (num > arr[mid])
return BSRev(arr, num, start, mid);
if (num < arr[mid])
return BSRev(arr, num, mid, end);
}
function findRotatedIndex(arr, num, start=0, end=arr.length - 1) {
if (arr[start] === num)
return start;
if (arr[end] === num)
return end;
if (end < start)
return -1;
if (num < arr[start] && num > arr[end])
return -1;
var mid = start + Math.floor((end - start) / 2);
if(mid === start && arr[mid] !== num) return -1;
if (arr[mid] === num)
return mid;
else {
if (arr[mid] > arr[start] && (num < arr[start] && num > arr[mid]))
return -1;
else if (arr[mid] > arr[start]) {
if (num > arr[start] && num < arr[mid])
return BS(arr, num, start, mid);
else
return findRotatedIndex(arr, num, mid + 1, end);
} else if (arr[mid] < arr[start]) {
if (num > arr[mid] && num<arr[end])
return BSRev(arr, num, mid, end);
else {
return findRotatedIndex(arr, num, start, mid - 1);
}
}
}
}