Skip to content

Commit ef5566f

Browse files
authored
Update BinarySearch.js (TheAlgorithms#209)
* Update BinarySearch.js The old algorithm didn't work, I believe for two main reasons: 1 - Number.MAX_VALUE is not a valid array index as it is used to represent the highest possible value in javascript (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_VALUE); 2 - splice() is not a pure function, every time it is called it has the side effect of modifying the original array (https://www.w3schools.com/jsref/jsref_splice.asp) ; So I rewrote the algorithm, it now returns an index ( -1 if not found ) and it works both on numbers and on strings. * Update BinarySearch.js Style change * Update BinarySearch.js Style change * Update BinarySearch.js
1 parent 744291b commit ef5566f

File tree

1 file changed

+59
-15
lines changed

1 file changed

+59
-15
lines changed

Search/BinarySearch.js

+59-15
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,70 @@
1-
/* Binary Search-Search a sorted array by repeatedly dividing the search interval
1+
/* Binary Search: https://en.wikipedia.org/wiki/Binary_search_algorithm
2+
*
3+
* Search a sorted array by repeatedly dividing the search interval
24
* in half. Begin with an interval covering the whole array. If the value of the
35
* search key is less than the item in the middle of the interval, narrow the interval
46
* to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the
57
* value is found or the interval is empty.
68
*/
79

8-
function binarySearch (arr, i) {
9-
var mid = Math.floor(arr.length / 2)
10-
if (arr[mid] === i) {
11-
console.log('match', arr[mid], i)
12-
return arr[mid]
13-
} else if (arr[mid] < i && arr.length > 1) {
14-
binarySearch(arr.splice(mid, Number.MAX_VALUE), i)
15-
} else if (arr[mid] > i && arr.length > 1) {
16-
binarySearch(arr.splice(0, mid), i)
10+
function binarySearch (arr, x, low = 0, high = arr.length - 1) {
11+
const mid = Math.floor(low + (high - low) / 2)
12+
13+
if (high >= low) {
14+
if (arr[mid] === x) {
15+
// item found => return its index
16+
return mid
17+
}
18+
19+
if (x < arr[mid]) {
20+
// arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid
21+
return binarySearch(arr, x, low, mid - 1)
22+
} else {
23+
// arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high
24+
return binarySearch(arr, x, mid + 1, high)
25+
}
1726
} else {
18-
console.log('not found', i)
27+
// if low > high => we have searched the whole array without finding the item
1928
return -1
2029
}
2130
}
2231

23-
var ar = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
24-
binarySearch(ar, 3)
25-
binarySearch(ar, 7)
26-
binarySearch(ar, 13)
32+
/* ---------------------------------- Test ---------------------------------- */
33+
34+
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
35+
const stringArr = [
36+
'Alpha',
37+
'Bravo',
38+
'Charlie',
39+
'Delta',
40+
'Echo',
41+
'Foxtrot',
42+
'Golf',
43+
'Hotel',
44+
'India',
45+
'Juliet',
46+
'Kilo',
47+
'Lima',
48+
'Mike',
49+
'November',
50+
'Oscar',
51+
'Papa',
52+
'Quebec',
53+
'Romeo',
54+
'Sierra',
55+
'Tango',
56+
'Uniform',
57+
'Victor',
58+
'Whiskey',
59+
'X-Ray',
60+
'Yankee',
61+
'Zulu'
62+
]
63+
64+
console.log(binarySearch(arr, 3))
65+
console.log(binarySearch(arr, 7))
66+
console.log(binarySearch(arr, 13))
67+
68+
console.log(binarySearch(stringArr, 'Charlie'))
69+
console.log(binarySearch(stringArr, 'Zulu'))
70+
console.log(binarySearch(stringArr, 'Sierra'))

0 commit comments

Comments
 (0)