From 660011c3f5d8baa93a12f4f47d168a7ba2929233 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 fbb292b1ca435f85b6c6065ad6c3d3c1da71ffe2 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 be08d9f7b1638a57c3cd85649e1cf45892f27d7d 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 8696edabf034503c0bed3fcdcfd5b96a7caca5d8 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 0825c161cd3ee9d3e10f0a74b977e399a535706a 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 9c35107655ccf69011bcffa597ae3eb9dd06f9aa 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 bfa7faf33966855d35c7c578e94164948c07d6a1 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