Skip to content

Commit 34a1eb2

Browse files
authored
Merge pull request #333 from RitikDua/master
Added BinarySearchIterative Function
2 parents 84e974c + fdeda72 commit 34a1eb2

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

Search/BinarySearch.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* value is found or the interval is empty.
88
*/
99

10-
function binarySearch (arr, x, low = 0, high = arr.length - 1) {
10+
function binarySearchRecursive (arr, x, low = 0, high = arr.length - 1) {
1111
const mid = Math.floor(low + (high - low) / 2)
1212

1313
if (high >= low) {
@@ -18,16 +18,36 @@ function binarySearch (arr, x, low = 0, high = arr.length - 1) {
1818

1919
if (x < arr[mid]) {
2020
// 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)
21+
return binarySearchRecursive(arr, x, low, mid - 1)
2222
} else {
2323
// 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)
24+
return binarySearchRecursive(arr, x, mid + 1, high)
2525
}
2626
} else {
2727
// if low > high => we have searched the whole array without finding the item
2828
return -1
2929
}
3030
}
31+
function binarySearchIterative (arr, x, low = 0, high = arr.length - 1) {
32+
while (high >= low) {
33+
const mid = Math.floor(low + (high - low) / 2)
34+
35+
if (arr[mid] === x) {
36+
// item found => return its index
37+
return mid
38+
}
39+
40+
if (x < arr[mid]) {
41+
// arr[mid] is an upper bound for x, so if x is in arr => low <= x < mid
42+
high = mid - 1
43+
} else {
44+
// arr[mid] is a lower bound for x, so if x is in arr => mid < x <= high
45+
low = mid + 1
46+
}
47+
}
48+
// if low > high => we have searched the whole array without finding the item
49+
return -1
50+
}
3151

3252
/* ---------------------------------- Test ---------------------------------- */
3353

@@ -61,10 +81,10 @@ const stringArr = [
6181
'Zulu'
6282
]
6383

64-
console.log(binarySearch(arr, 3))
65-
console.log(binarySearch(arr, 7))
66-
console.log(binarySearch(arr, 13))
84+
console.log(binarySearchRecursive(arr, 3))
85+
console.log(binarySearchIterative(arr, 7))
86+
console.log(binarySearchRecursive(arr, 13))
6787

68-
console.log(binarySearch(stringArr, 'Charlie'))
69-
console.log(binarySearch(stringArr, 'Zulu'))
70-
console.log(binarySearch(stringArr, 'Sierra'))
88+
console.log(binarySearchIterative(stringArr, 'Charlie'))
89+
console.log(binarySearchRecursive(stringArr, 'Zulu'))
90+
console.log(binarySearchIterative(stringArr, 'Sierra'))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy