From 1c8fb603dd353b51114c632d3c57528c1ad75a8d Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Tue, 19 Apr 2022 16:27:06 -0300 Subject: [PATCH 1/9] feat: added alternative binary search --- Search/BinarySearch.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/Search/BinarySearch.js b/Search/BinarySearch.js index c822cfac60..9ebbede562 100644 --- a/Search/BinarySearch.js +++ b/Search/BinarySearch.js @@ -49,7 +49,23 @@ function binarySearchIterative (arr, x, low = 0, high = arr.length - 1) { return -1 } -export { binarySearchIterative, binarySearchRecursive } +function binarySearchIterativeAlternative(arr, x) { + let low = -1, + high = arr.length + while (low < high - 1) { + const half = Math.floor((low + high) / 2) + // arr[half] === x => low = half else high = half + arr[half] < x ? (low = half) : (high = half) + } + // if arr[high] === x => return high else return -1 + return arr[high] === x ? dir : -1 +} + +export { + binarySearchIterative, + binarySearchIterativeAlternative, + binarySearchRecursive +} /* ---------------------------------- Test ---------------------------------- */ @@ -89,3 +105,10 @@ export { binarySearchIterative, binarySearchRecursive } // binarySearchIterative(stringArr, 'Charlie') // binarySearchRecursive(stringArr, 'Zulu') // binarySearchIterative(stringArr, 'Sierra') +// console.log("------------------------------------------") +// console.log(binarySearchIterativeAlternative(arr, 3)) +// console.log(binarySearchIterativeAlternative(arr, 7)) +// console.log(binarySearchIterativeAlternative(arr, 13)) +// console.log(binarySearchIterative(stringArr, 'Charlie')) +// console.log(binarySearchRecursive(stringArr, 'Zulu')) +// console.log(binarySearchIterative(stringArr, 'Sierra')) From dbab8f3c2dd6f085eb11f8a90fa7957b64f29046 Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Tue, 19 Apr 2022 16:31:37 -0300 Subject: [PATCH 2/9] fix: exchange "dir" for "high" --- Search/BinarySearch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Search/BinarySearch.js b/Search/BinarySearch.js index 9ebbede562..c9083f715d 100644 --- a/Search/BinarySearch.js +++ b/Search/BinarySearch.js @@ -58,7 +58,7 @@ function binarySearchIterativeAlternative(arr, x) { arr[half] < x ? (low = half) : (high = half) } // if arr[high] === x => return high else return -1 - return arr[high] === x ? dir : -1 + return arr[high] === x ? high : -1 } export { From a094a81aa4f16f29150d338e34d1b6c0c25168e4 Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Tue, 19 Apr 2022 16:42:03 -0300 Subject: [PATCH 3/9] fix : fixing code style --- Search/BinarySearch.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Search/BinarySearch.js b/Search/BinarySearch.js index c9083f715d..0e826d9a9c 100644 --- a/Search/BinarySearch.js +++ b/Search/BinarySearch.js @@ -48,10 +48,9 @@ function binarySearchIterative (arr, x, low = 0, high = arr.length - 1) { // if low > high => we have searched the whole array without finding the item return -1 } - -function binarySearchIterativeAlternative(arr, x) { - let low = -1, - high = arr.length +function binarySearchIterativeAlternative (arr, x) { + let low = -1 + let high = arr.length while (low < high - 1) { const half = Math.floor((low + high) / 2) // arr[half] === x => low = half else high = half From 1a568ad087c03d77aa1b275c017221468eff6c6f Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Wed, 20 Apr 2022 12:09:49 -0300 Subject: [PATCH 4/9] fix: fixed readability --- Search/BinarySearch.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Search/BinarySearch.js b/Search/BinarySearch.js index 0e826d9a9c..fd4ac758c5 100644 --- a/Search/BinarySearch.js +++ b/Search/BinarySearch.js @@ -52,11 +52,13 @@ function binarySearchIterativeAlternative (arr, x) { let low = -1 let high = arr.length while (low < high - 1) { - const half = Math.floor((low + high) / 2) - // arr[half] === x => low = half else high = half - arr[half] < x ? (low = half) : (high = half) + const mid = Math.floor((low + high) / 2) + if (arr[mid] < x) { + low = mid + } else { + high = mid + } } - // if arr[high] === x => return high else return -1 return arr[high] === x ? high : -1 } From f1fd3c9878b1760e6925171af0eedb8aae7947bb Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Wed, 20 Apr 2022 12:15:54 -0300 Subject: [PATCH 5/9] fix: fixed code smells --- Search/BinarySearch.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Search/BinarySearch.js b/Search/BinarySearch.js index fd4ac758c5..4102857d30 100644 --- a/Search/BinarySearch.js +++ b/Search/BinarySearch.js @@ -106,10 +106,9 @@ export { // binarySearchIterative(stringArr, 'Charlie') // binarySearchRecursive(stringArr, 'Zulu') // binarySearchIterative(stringArr, 'Sierra') -// console.log("------------------------------------------") -// console.log(binarySearchIterativeAlternative(arr, 3)) -// console.log(binarySearchIterativeAlternative(arr, 7)) -// console.log(binarySearchIterativeAlternative(arr, 13)) -// console.log(binarySearchIterative(stringArr, 'Charlie')) -// console.log(binarySearchRecursive(stringArr, 'Zulu')) -// console.log(binarySearchIterative(stringArr, 'Sierra')) +// binarySearchIterativeAlternative(arr, 3) +// binarySearchIterativeAlternative(arr, 7) +// binarySearchIterativeAlternative(arr, 13) +// binarySearchIterativeAlternative(stringArr, 'Charlie') +// binarySearchIterativeAlternative(stringArr, 'Zulu') +// binarySearchIterativeAlternative(stringArr, 'Sierra') From a9b8c05c2b37b393ee1a0f4c3a32e73c4f2be236 Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Wed, 20 Apr 2022 16:32:39 -0300 Subject: [PATCH 6/9] fix: remove binary search alternative --- Search/BinarySearch.js | 59 ------------------------------------------ 1 file changed, 59 deletions(-) diff --git a/Search/BinarySearch.js b/Search/BinarySearch.js index 4102857d30..e4b649408f 100644 --- a/Search/BinarySearch.js +++ b/Search/BinarySearch.js @@ -48,67 +48,8 @@ function binarySearchIterative (arr, x, low = 0, high = arr.length - 1) { // if low > high => we have searched the whole array without finding the item return -1 } -function binarySearchIterativeAlternative (arr, x) { - let low = -1 - let high = arr.length - while (low < high - 1) { - const mid = Math.floor((low + high) / 2) - if (arr[mid] < x) { - low = mid - } else { - high = mid - } - } - return arr[high] === x ? high : -1 -} export { binarySearchIterative, - binarySearchIterativeAlternative, binarySearchRecursive } - -/* ---------------------------------- 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' -// ] - -// binarySearchRecursive(arr, 3) -// binarySearchIterative(arr, 7) -// binarySearchRecursive(arr, 13) -// binarySearchIterative(stringArr, 'Charlie') -// binarySearchRecursive(stringArr, 'Zulu') -// binarySearchIterative(stringArr, 'Sierra') -// binarySearchIterativeAlternative(arr, 3) -// binarySearchIterativeAlternative(arr, 7) -// binarySearchIterativeAlternative(arr, 13) -// binarySearchIterativeAlternative(stringArr, 'Charlie') -// binarySearchIterativeAlternative(stringArr, 'Zulu') -// binarySearchIterativeAlternative(stringArr, 'Sierra') From 459ddef51eb301be92dcaf3ef5699cccddb6c9f8 Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Wed, 20 Apr 2022 16:34:36 -0300 Subject: [PATCH 7/9] feat: added tests of binary search interative and recursive --- Search/test/BinarySearch.test.js | 61 ++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Search/test/BinarySearch.test.js diff --git a/Search/test/BinarySearch.test.js b/Search/test/BinarySearch.test.js new file mode 100644 index 0000000000..7b4c49853f --- /dev/null +++ b/Search/test/BinarySearch.test.js @@ -0,0 +1,61 @@ +import { binarySearchIterative, binarySearchRecursive } from '../BinarySearch' + +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' +] + +describe('Binary Search Iterative test', () => { + test('expect to return the index of the item in the array', () => { + expect(binarySearchIterative(arr, 3)).toBe(2) + }) + test('expect to return -1 if not in array', () => { + expect(binarySearchIterative(arr, 11)).toBe(-1) + }) + test('expect to return the index of the item in the array', () => { + expect(binarySearchIterative(stringArr, 'Charlie')).toBe(2) + }) + test('expect to return -1 if not in array', () => { + expect(binarySearchIterative(stringArr, 'Zoft')).toBe(-1) + }) +}) + +describe('Binary Search Recursive test', () => { + test('expect to return the index of the item in the array', () => { + expect(binarySearchRecursive(arr, 3)).toBe(2) + }) + test('expect to return -1 if not in array', () => { + expect(binarySearchRecursive(arr, 11)).toBe(-1) + }) + test('expect to return the index of the item in the array', () => { + expect(binarySearchRecursive(stringArr, 'Charlie')).toBe(2) + }) + test('expect to return -1 if not in array', () => { + expect(binarySearchRecursive(stringArr, 'Zoft')).toBe(-1) + }) +}) From 01a2e0d1a3677440853bf8ca8b9409666cbf9485 Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Wed, 20 Apr 2022 16:37:13 -0300 Subject: [PATCH 8/9] fix: fixed wrong identation --- Search/BinarySearch.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Search/BinarySearch.js b/Search/BinarySearch.js index e4b649408f..89cb37f88d 100644 --- a/Search/BinarySearch.js +++ b/Search/BinarySearch.js @@ -49,7 +49,4 @@ function binarySearchIterative (arr, x, low = 0, high = arr.length - 1) { return -1 } -export { - binarySearchIterative, - binarySearchRecursive -} +export { binarySearchIterative, binarySearchRecursive } From ea67fc62382a05f3ef6d5fe6d31a09ad1c415a6d Mon Sep 17 00:00:00 2001 From: CarlosZoft Date: Wed, 20 Apr 2022 17:00:07 -0300 Subject: [PATCH 9/9] fix: refactoring duplicated code of tests --- Search/test/BinarySearch.test.js | 44 ++++++++++++-------------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/Search/test/BinarySearch.test.js b/Search/test/BinarySearch.test.js index 7b4c49853f..a903b4a24e 100644 --- a/Search/test/BinarySearch.test.js +++ b/Search/test/BinarySearch.test.js @@ -30,32 +30,20 @@ const stringArr = [ 'Zulu' ] -describe('Binary Search Iterative test', () => { - test('expect to return the index of the item in the array', () => { - expect(binarySearchIterative(arr, 3)).toBe(2) - }) - test('expect to return -1 if not in array', () => { - expect(binarySearchIterative(arr, 11)).toBe(-1) - }) - test('expect to return the index of the item in the array', () => { - expect(binarySearchIterative(stringArr, 'Charlie')).toBe(2) - }) - test('expect to return -1 if not in array', () => { - expect(binarySearchIterative(stringArr, 'Zoft')).toBe(-1) - }) -}) - -describe('Binary Search Recursive test', () => { - test('expect to return the index of the item in the array', () => { - expect(binarySearchRecursive(arr, 3)).toBe(2) - }) - test('expect to return -1 if not in array', () => { - expect(binarySearchRecursive(arr, 11)).toBe(-1) - }) - test('expect to return the index of the item in the array', () => { - expect(binarySearchRecursive(stringArr, 'Charlie')).toBe(2) - }) - test('expect to return -1 if not in array', () => { - expect(binarySearchRecursive(stringArr, 'Zoft')).toBe(-1) - }) +describe('Binary Search', () => { + const funcs = [binarySearchIterative, binarySearchRecursive] + for (const func of funcs) { + test('expect to return the index of the item in the array', () => { + expect(func(arr, 3)).toBe(2) + }) + test('expect to return -1 if not in array', () => { + expect(func(arr, 11)).toBe(-1) + }) + test('expect to return the index of the item in the array', () => { + expect(func(stringArr, 'Charlie')).toBe(2) + }) + test('expect to return -1 if not in array', () => { + expect(func(stringArr, 'Zoft')).toBe(-1) + }) + } })