mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
Update BinarySearch.js (#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
This commit is contained in:
@ -1,26 +1,70 @@
|
|||||||
/* Binary Search-Search a sorted array by repeatedly dividing the search interval
|
/* Binary Search: https://en.wikipedia.org/wiki/Binary_search_algorithm
|
||||||
|
*
|
||||||
|
* Search a sorted array by repeatedly dividing the search interval
|
||||||
* in half. Begin with an interval covering the whole array. If the value of the
|
* in half. Begin with an interval covering the whole array. If the value of the
|
||||||
* search key is less than the item in the middle of the interval, narrow the interval
|
* search key is less than the item in the middle of the interval, narrow the interval
|
||||||
* to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the
|
* to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the
|
||||||
* value is found or the interval is empty.
|
* value is found or the interval is empty.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function binarySearch (arr, i) {
|
function binarySearch (arr, x, low = 0, high = arr.length - 1) {
|
||||||
var mid = Math.floor(arr.length / 2)
|
const mid = Math.floor(low + (high - low) / 2)
|
||||||
if (arr[mid] === i) {
|
|
||||||
console.log('match', arr[mid], i)
|
if (high >= low) {
|
||||||
return arr[mid]
|
if (arr[mid] === x) {
|
||||||
} else if (arr[mid] < i && arr.length > 1) {
|
// item found => return its index
|
||||||
binarySearch(arr.splice(mid, Number.MAX_VALUE), i)
|
return mid
|
||||||
} else if (arr[mid] > i && arr.length > 1) {
|
}
|
||||||
binarySearch(arr.splice(0, mid), i)
|
|
||||||
|
if (x < arr[mid]) {
|
||||||
|
// arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid
|
||||||
|
return binarySearch(arr, x, low, mid - 1)
|
||||||
|
} else {
|
||||||
|
// arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high
|
||||||
|
return binarySearch(arr, x, mid + 1, high)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
console.log('not found', i)
|
// if low > high => we have searched the whole array without finding the item
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var ar = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
/* ---------------------------------- Test ---------------------------------- */
|
||||||
binarySearch(ar, 3)
|
|
||||||
binarySearch(ar, 7)
|
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||||
binarySearch(ar, 13)
|
const stringArr = [
|
||||||
|
'Alpha',
|
||||||
|
'Bravo',
|
||||||
|
'Charlie',
|
||||||
|
'Delta',
|
||||||
|
'Echo',
|
||||||
|
'Foxtrot',
|
||||||
|
'Golf',
|
||||||
|
'Hotel',
|
||||||
|
'India',
|
||||||
|
'Juliet',
|
||||||
|
'Kilo',
|
||||||
|
'Lima',
|
||||||
|
'Mike',
|
||||||
|
'November',
|
||||||
|
'Oscar',
|
||||||
|
'Papa',
|
||||||
|
'Quebec',
|
||||||
|
'Romeo',
|
||||||
|
'Sierra',
|
||||||
|
'Tango',
|
||||||
|
'Uniform',
|
||||||
|
'Victor',
|
||||||
|
'Whiskey',
|
||||||
|
'X-Ray',
|
||||||
|
'Yankee',
|
||||||
|
'Zulu'
|
||||||
|
]
|
||||||
|
|
||||||
|
console.log(binarySearch(arr, 3))
|
||||||
|
console.log(binarySearch(arr, 7))
|
||||||
|
console.log(binarySearch(arr, 13))
|
||||||
|
|
||||||
|
console.log(binarySearch(stringArr, 'Charlie'))
|
||||||
|
console.log(binarySearch(stringArr, 'Zulu'))
|
||||||
|
console.log(binarySearch(stringArr, 'Sierra'))
|
||||||
|
Reference in New Issue
Block a user