From af77ab8f2f512c666e5c35216af3dd8e4b35fa54 Mon Sep 17 00:00:00 2001 From: Askanders <47815058+Askanders@users.noreply.github.com> Date: Sat, 27 Jun 2020 16:43:43 +0200 Subject: [PATCH 1/4] 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. --- Search/BinarySearch.js | 76 +++++++++++++++++++++++++++++++++--------- 1 file changed, 60 insertions(+), 16 deletions(-) diff --git a/Search/BinarySearch.js b/Search/BinarySearch.js index ed4dfa79a1..00ebf73528 100644 --- a/Search/BinarySearch.js +++ b/Search/BinarySearch.js @@ -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 * 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 * value is found or the interval is empty. */ -function binarySearch (arr, i) { - var mid = Math.floor(arr.length / 2) - if (arr[mid] === i) { - console.log('match', arr[mid], i) - return arr[mid] - } else if (arr[mid] < i && arr.length > 1) { - binarySearch(arr.splice(mid, Number.MAX_VALUE), i) - } else if (arr[mid] > i && arr.length > 1) { - binarySearch(arr.splice(0, mid), i) +function binarySearch(arr, x, low = 0, high = arr.length - 1) { + let mid = Math.floor(low + (high - low) / 2); + + if (high >= low) { + if (arr[mid] === x) { + // item found => return its index + return mid; + } + + 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 { - console.log('not found', i) - return -1 + // if low > high => we have searched the whole array without finding the item + return -1; } } -var ar = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] -binarySearch(ar, 3) -binarySearch(ar, 7) -binarySearch(ar, 13) +/* ---------------------------------- Test ---------------------------------- */ + +const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; +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")); From ee9a03d48a85f7c7f66bec148569468d7510a2da Mon Sep 17 00:00:00 2001 From: Askanders <47815058+Askanders@users.noreply.github.com> Date: Sat, 27 Jun 2020 17:11:02 +0200 Subject: [PATCH 2/4] Update BinarySearch.js Style change --- Search/BinarySearch.js | 76 +++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/Search/BinarySearch.js b/Search/BinarySearch.js index 00ebf73528..6674f89ddb 100644 --- a/Search/BinarySearch.js +++ b/Search/BinarySearch.js @@ -7,25 +7,25 @@ * value is found or the interval is empty. */ -function binarySearch(arr, x, low = 0, high = arr.length - 1) { - let mid = Math.floor(low + (high - low) / 2); +function binarySearch( arr, x, low = 0, high = arr.length - 1 ) { + const mid = Math.floor(low + (high - low) / 2) if (high >= low) { if (arr[mid] === x) { // item found => return its index - return mid; + return mid } 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); + 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); + return binarySearch(arr, x, mid + 1, high) } } else { // if low > high => we have searched the whole array without finding the item - return -1; + return -1 } } @@ -33,38 +33,38 @@ function binarySearch(arr, x, low = 0, high = arr.length - 1) { const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; 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", + '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(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")); +console.log(binarySearch(stringArr, "Charlie")) +console.log(binarySearch(stringArr, "Zulu")) +console.log(binarySearch(stringArr, "Sierra")) From 47d18e38c3daf5c262d770139fa3f27a6aa0b741 Mon Sep 17 00:00:00 2001 From: Askanders <47815058+Askanders@users.noreply.github.com> Date: Sat, 27 Jun 2020 17:15:54 +0200 Subject: [PATCH 3/4] Update BinarySearch.js Style change --- Search/BinarySearch.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Search/BinarySearch.js b/Search/BinarySearch.js index 6674f89ddb..26a3ceae74 100644 --- a/Search/BinarySearch.js +++ b/Search/BinarySearch.js @@ -7,7 +7,7 @@ * value is found or the interval is empty. */ -function binarySearch( arr, x, low = 0, high = arr.length - 1 ) { +function binarySearch (arr, x, low = 0, high = arr.length - 1){ const mid = Math.floor(low + (high - low) / 2) if (high >= low) { @@ -31,7 +31,7 @@ function binarySearch( arr, x, low = 0, high = arr.length - 1 ) { /* ---------------------------------- Test ---------------------------------- */ -const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; +const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] const stringArr = [ 'Alpha', 'Bravo', @@ -58,13 +58,13 @@ const stringArr = [ 'Whiskey', 'X-Ray', 'Yankee', - 'Zulu', -]; + '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")) +console.log(binarySearch(stringArr, 'Charlie')) +console.log(binarySearch(stringArr, 'Zulu')) +console.log(binarySearch(stringArr, 'Sierra')) From fc79506dd331cc1844d9a56addfefe9f23c5dfa3 Mon Sep 17 00:00:00 2001 From: Askanders <47815058+Askanders@users.noreply.github.com> Date: Sat, 27 Jun 2020 17:18:43 +0200 Subject: [PATCH 4/4] Update BinarySearch.js --- Search/BinarySearch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Search/BinarySearch.js b/Search/BinarySearch.js index 26a3ceae74..3a150d6384 100644 --- a/Search/BinarySearch.js +++ b/Search/BinarySearch.js @@ -7,7 +7,7 @@ * value is found or the interval is empty. */ -function binarySearch (arr, x, low = 0, high = arr.length - 1){ +function binarySearch (arr, x, low = 0, high = arr.length - 1) { const mid = Math.floor(low + (high - low) / 2) if (high >= low) {