-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNearlySorted.js
More file actions
27 lines (22 loc) · 851 Bytes
/
NearlySorted.js
File metadata and controls
27 lines (22 loc) · 851 Bytes
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
var binarySearch = function(low, high, nums, target) {
if (high < low) return -1;
var mid = Math.floor((high + low)/2);
if (nums[mid] === target) return mid;
if (nums[mid - 1] === target) return mid - 1;
if (nums[mid + 1] === target) return mid + 1;
console.log(nums[low], nums[mid], target);
if (nums[mid] > nums[mid - 1] && nums[mid] < nums[mid + 1]) {
index = mid;
} else if (nums[mid] < nums[mid - 1] && nums[mid] < nums[mid + 1]) {
index = mid - 1;
} else {
index = mid + 1;
}
if (target < nums[index]) {
return binarySearch(low, index - 1, nums, target);
}
return binarySearch(mid+1, high, nums, target);}
var search = function(nums, target) {
return binarySearch(0, nums.length - 1, nums, target);
};
console.log(search([2, 1, 3, 5, 4, 7, 6, 8, 9], 9));