|
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 |
2 | 4 | * in half. Begin with an interval covering the whole array. If the value of the
|
3 | 5 | * search key is less than the item in the middle of the interval, narrow the interval
|
4 | 6 | * to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the
|
5 | 7 | * value is found or the interval is empty.
|
6 | 8 | */
|
7 | 9 |
|
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 | + } |
17 | 26 | } else {
|
18 |
| - console.log('not found', i) |
| 27 | + // if low > high => we have searched the whole array without finding the item |
19 | 28 | return -1
|
20 | 29 | }
|
21 | 30 | }
|
22 | 31 |
|
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