From e42a2265e0651a40c65f9829c4908eee2b053d13 Mon Sep 17 00:00:00 2001 From: King Date: Fri, 5 Jan 2018 10:32:10 -0500 Subject: [PATCH 1/7] add binarySearch --- snippets/binarySearch.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 snippets/binarySearch.md diff --git a/snippets/binarySearch.md b/snippets/binarySearch.md new file mode 100644 index 000000000..0af272a6c --- /dev/null +++ b/snippets/binarySearch.md @@ -0,0 +1,23 @@ +### binarySearch + +Use recursion to perform a logarithmic search similar to `.indexOf()` that finds the index of a value within an array. The two differences being +1. Operation only works with sorted arrays +2. Offers a major performance boost when compared to a linear search or `.indexOf()` + +```js +const binarySearch = (arr, val, start = 0, end = arr.length - 1) => { + if (start > end) return -1; + const mid = Math.floor((start + end) / 2); + const target = arr[mid]; + return (target > val) + ? binarySearch(arr, val, start, mid - 1) + : (target < val) + ? binarySearch(arr, val, mid + 1, end) + : mid +} +``` + +```js +binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 6); // 2 +binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 21); // -1 +``` From 6e3dc25d93679445f61e7f86cb3517c39e1b41c9 Mon Sep 17 00:00:00 2001 From: King Date: Fri, 5 Jan 2018 10:33:09 -0500 Subject: [PATCH 2/7] run npm tagger & tag binarySearch:array --- tag_database | 1 + 1 file changed, 1 insertion(+) diff --git a/tag_database b/tag_database index 71d8ccba9..fad61fe15 100644 --- a/tag_database +++ b/tag_database @@ -1,6 +1,7 @@ anagrams:string,recursion arrayToHtmlList:browser,array average:math,array +binarySearch:array bottomVisible:browser byteSize:string call:adapter,function From 1427a6f50af2a15d9e98e1ce0f77948c52ed3200 Mon Sep 17 00:00:00 2001 From: King Date: Fri, 5 Jan 2018 11:18:01 -0500 Subject: [PATCH 3/7] replace target with arr[mid] --- snippets/binarySearch.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/snippets/binarySearch.md b/snippets/binarySearch.md index 0af272a6c..c34ba397e 100644 --- a/snippets/binarySearch.md +++ b/snippets/binarySearch.md @@ -1,17 +1,16 @@ ### binarySearch -Use recursion to perform a logarithmic search similar to `.indexOf()` that finds the index of a value within an array. The two differences being +Use recursion. Similar to `Array.indexOf()` that finds the index of a value within an array. The two differences being 1. Operation only works with sorted arrays -2. Offers a major performance boost when compared to a linear search or `.indexOf()` +2. Offers a major performance boost when compared to a linear search or `Array.indexOf()` ```js const binarySearch = (arr, val, start = 0, end = arr.length - 1) => { if (start > end) return -1; const mid = Math.floor((start + end) / 2); - const target = arr[mid]; - return (target > val) + return (arr[mid] > val) ? binarySearch(arr, val, start, mid - 1) - : (target < val) + : (arr[mid] < val) ? binarySearch(arr, val, mid + 1, end) : mid } From db092d808e227fb2af85b7d2a463ec6608fc5cfa Mon Sep 17 00:00:00 2001 From: King Date: Fri, 5 Jan 2018 11:29:14 -0500 Subject: [PATCH 4/7] update description --- snippets/binarySearch.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/binarySearch.md b/snippets/binarySearch.md index c34ba397e..9f64191ef 100644 --- a/snippets/binarySearch.md +++ b/snippets/binarySearch.md @@ -1,8 +1,8 @@ ### binarySearch -Use recursion. Similar to `Array.indexOf()` that finds the index of a value within an array. The two differences being -1. Operation only works with sorted arrays -2. Offers a major performance boost when compared to a linear search or `Array.indexOf()` +Use recursion. Similar to `Array.indexOf()` that finds the index of a value within an array. The difference being operation only works with sorted arrays which offers a major performance boost due to it's logarithmic nature when compared to a linear search or `Array.indexOf()` + +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 is less than the item in the middle of the interval, recurse into the lower half. Otherwise recurse into the upper half. Repeatedly recurse until the value is found which is the mid or you've recursed to a point that is greater than the length which means the value doesn't exist and return `-1`. ```js const binarySearch = (arr, val, start = 0, end = arr.length - 1) => { From 392edcd45bd429adc26f6fd9fb4894183839629a Mon Sep 17 00:00:00 2001 From: King Date: Sat, 6 Jan 2018 08:47:30 -0500 Subject: [PATCH 5/7] udpate binarySeach with if + return oneline --- snippets/binarySearch.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/snippets/binarySearch.md b/snippets/binarySearch.md index 9f64191ef..6f35179e0 100644 --- a/snippets/binarySearch.md +++ b/snippets/binarySearch.md @@ -8,13 +8,11 @@ Search a sorted array by repeatedly dividing the search interval in half. Begin const binarySearch = (arr, val, start = 0, end = arr.length - 1) => { if (start > end) return -1; const mid = Math.floor((start + end) / 2); - return (arr[mid] > val) - ? binarySearch(arr, val, start, mid - 1) - : (arr[mid] < val) - ? binarySearch(arr, val, mid + 1, end) - : mid + if (arr[mid] > val) return binarySearch(arr, val, start, mid - 1); + if (arr[mid] < val) return binarySearch(arr, val, mid + 1, end); + return mid; } -``` +``` ```js binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 6); // 2 From 3e343677ce745db371f9bdea11458695db9b68e5 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 9 Jan 2018 01:19:11 +0200 Subject: [PATCH 6/7] Update binarySearch.md --- snippets/binarySearch.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/snippets/binarySearch.md b/snippets/binarySearch.md index 6f35179e0..3645954cb 100644 --- a/snippets/binarySearch.md +++ b/snippets/binarySearch.md @@ -1,8 +1,12 @@ ### binarySearch -Use recursion. Similar to `Array.indexOf()` that finds the index of a value within an array. The difference being operation only works with sorted arrays which offers a major performance boost due to it's logarithmic nature when compared to a linear search or `Array.indexOf()` +Use recursion. Similar to `Array.indexOf()` that finds the index of a value within an array. +The difference being this operation only works with sorted arrays which offers a major performance boost due to it's logarithmic nature when compared to a linear search or `Array.indexOf()`. -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 is less than the item in the middle of the interval, recurse into the lower half. Otherwise recurse into the upper half. Repeatedly recurse until the value is found which is the mid or you've recursed to a point that is greater than the length which means the value doesn't exist and return `-1`. +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 is less than the item in the middle of the interval, recurse into the lower half. Otherwise recurse into the upper half. +Repeatedly recurse until the value is found which is the mid or you've recursed to a point that is greater than the length which means the value doesn't exist and return `-1`. ```js const binarySearch = (arr, val, start = 0, end = arr.length - 1) => { From 8b24e46b7247151aa87952f6d7ba558d74696f8d Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 9 Jan 2018 01:20:17 +0200 Subject: [PATCH 7/7] Archived binarySearch.md --- {snippets => snippets_archive}/binarySearch.md | 0 tag_database | 1 - 2 files changed, 1 deletion(-) rename {snippets => snippets_archive}/binarySearch.md (100%) diff --git a/snippets/binarySearch.md b/snippets_archive/binarySearch.md similarity index 100% rename from snippets/binarySearch.md rename to snippets_archive/binarySearch.md diff --git a/tag_database b/tag_database index fad61fe15..71d8ccba9 100644 --- a/tag_database +++ b/tag_database @@ -1,7 +1,6 @@ anagrams:string,recursion arrayToHtmlList:browser,array average:math,array -binarySearch:array bottomVisible:browser byteSize:string call:adapter,function