-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarySearch.js
203 lines (163 loc) · 4.57 KB
/
binarySearch.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
function binarySearch (arr = [], target) {
let lo = 0
let hi = arr.length - 1
let mid
while (lo <= hi) {
mid = lo + Math.floor((hi - lo) / 2)
// Found it
if (arr[mid] === target) {
return mid
// Too high
} else if (arr[mid] > target) {
hi = mid - 1
// Too low
} else if (arr[mid] < target) {
lo = mid + 1
}
}
// Didn't find it
return -1
}
function binarySearchRecursive (arr = [], target, lo = 0, hi = arr.length - 1) {
const mid = lo + Math.floor((hi - lo) / 2)
// Base case: found it
if (arr[mid] === target) {
return mid
}
// Base case: didn't find it
if (lo >= hi) {
return -1
}
// Recursive case: search lower
if (arr[mid] > target) {
return binarySearchRecursive(arr, target, lo, mid - 1)
}
// Recursive case: search higher
if (arr[mid] < target) {
return binarySearchRecursive(arr, target, mid + 1, hi)
}
}
function binarySearchClosure (arr = [], target) {
const lo = 0
const hi = arr.length - 1
return (function binarySearchInner (lo, hi) {
const mid = lo + Math.floor((hi - lo) / 2)
// Base case: found it
if (arr[mid] === target) {
return mid
}
// Base case: didn't find it
if (lo >= hi) {
return -1
}
// Recursive case: search lower
if (arr[mid] > target) {
return binarySearchInner(lo, mid - 1)
}
// Recursive case: search higher
if (arr[mid] < target) {
return binarySearchInner(mid + 1, hi)
}
}(lo, hi))
}
function binarySearchFirst (arr = [], target) {
let lo = 0
let hi = arr.length - 1
let mid
let result = -1
while (lo <= hi) {
mid = lo + Math.floor((hi - lo) / 2)
// Found a match
if (arr[mid] === target) {
result = mid
}
// Too high or same
if (arr[mid] >= target) {
hi = mid - 1
// Too low
} else if (arr[mid] < target) {
lo = mid + 1
}
}
// Return best match
return result
}
function binarySearchLast (arr = [], target) {
let lo = 0
let hi = arr.length - 1
let mid
let result = -1
while (lo <= hi) {
mid = lo + Math.floor((hi - lo) / 2)
// Found a match
if (arr[mid] === target) {
result = mid
}
// Too high
if (arr[mid] > target) {
hi = mid - 1
// Too low or same
} else if (arr[mid] <= target) {
lo = mid + 1
}
}
// Return best match
return result
}
// Find where to insert a given target
// If the value already exists, insert after it
function binarySearchInsert (arr = [], target) {
let lo = 0
let hi = arr.length - 1
let mid
while (lo <= hi) {
mid = lo + Math.floor((hi - lo) / 2)
// Too high
if (arr[mid] > target) {
hi = mid - 1
// Too low or same
} else if (arr[mid] <= target) {
lo = mid + 1
}
}
// Go with the higher index that we landed on
return Math.max(lo, hi)
}
const arr = [1, 3, 3, 5, 7, 11, 11, 11, 13, 17, 19, 23, 29, 31, 33, 33, 33, 37, 41, 43, 47, 53, 59]
console.log(arr)
console.log('\nbinarySearch')
console.log('1: ' + binarySearch(arr, 1))
console.log('2: ' + binarySearch(arr, 2))
console.log('3: ' + binarySearch(arr, 3))
console.log('11: ' + binarySearch(arr, 11))
console.log('33: ' + binarySearch(arr, 33))
console.log('\nbinarySearchRecursive')
console.log('1: ' + binarySearchRecursive(arr, 1))
console.log('2: ' + binarySearchRecursive(arr, 2))
console.log('3: ' + binarySearchRecursive(arr, 3))
console.log('11: ' + binarySearchFirst(arr, 11))
console.log('33: ' + binarySearchFirst(arr, 33))
console.log('\nbinarySearchClosure')
console.log('1: ' + binarySearchClosure(arr, 1))
console.log('2: ' + binarySearchClosure(arr, 2))
console.log('3: ' + binarySearchClosure(arr, 3))
console.log('11: ' + binarySearchFirst(arr, 11))
console.log('33: ' + binarySearchFirst(arr, 33))
console.log('\nbinarySearchFirst')
console.log('1: ' + binarySearchFirst(arr, 1))
console.log('2: ' + binarySearchFirst(arr, 2))
console.log('3: ' + binarySearchFirst(arr, 3))
console.log('11: ' + binarySearchFirst(arr, 11))
console.log('33: ' + binarySearchFirst(arr, 33))
console.log('\nbinarySearchLast')
console.log('1: ' + binarySearchLast(arr, 1))
console.log('2: ' + binarySearchLast(arr, 2))
console.log('3: ' + binarySearchLast(arr, 3))
console.log('11: ' + binarySearchLast(arr, 11))
console.log('33: ' + binarySearchLast(arr, 33))
console.log('\nbinarySearchInsert')
console.log('1: ' + binarySearchInsert(arr, 1))
console.log('2: ' + binarySearchInsert(arr, 2))
console.log('3: ' + binarySearchInsert(arr, 3))
console.log('11: ' + binarySearchInsert(arr, 11))
console.log('33: ' + binarySearchInsert(arr, 33))