From 55080ebffd32f611fbde0121de79ab98201b3b59 Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Mon, 4 Oct 2021 17:56:51 -0400 Subject: [PATCH 1/7] Add a reference link for BucketSort --- Sorts/BucketSort.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/BucketSort.js b/Sorts/BucketSort.js index 9423c4c10..0a35d986c 100644 --- a/Sorts/BucketSort.js +++ b/Sorts/BucketSort.js @@ -1,5 +1,5 @@ /* -Wikipedia says: Bucket sort, or bin sort, is a sorting algorithm that works by distributing the +[Wikipedia says](https://en.wikipedia.org/wiki/Bucket_sort#:~:text=Bucket%20sort%2C%20or%20bin%20sort,applying%20the%20bucket%20sorting%20algorithm.&text=Sort%20each%20non%2Dempty%20bucket.): Bucket sort, or bin sort, is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm. It is a distribution sort, and is a cousin of radix sort in the most to least significant digit flavour. From 754f7b6b0420c71bec7692ac03064507b9e77375 Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Mon, 4 Oct 2021 17:57:13 -0400 Subject: [PATCH 2/7] Export bucketSort function for Jest testing --- Sorts/BucketSort.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sorts/BucketSort.js b/Sorts/BucketSort.js index 0a35d986c..bfdf5e350 100644 --- a/Sorts/BucketSort.js +++ b/Sorts/BucketSort.js @@ -62,3 +62,5 @@ console.log(arrOrignal) const arrSorted = bucketSort(arrOrignal) // Array after sort console.log(arrSorted) + +export { bucketSort } From 269175689f9722200ddf9d3bd0a9d093e67ea2fb Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Mon, 4 Oct 2021 18:27:17 -0400 Subject: [PATCH 3/7] Add function documentation to bucketSort --- Sorts/BucketSort.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Sorts/BucketSort.js b/Sorts/BucketSort.js index bfdf5e350..779cd2d62 100644 --- a/Sorts/BucketSort.js +++ b/Sorts/BucketSort.js @@ -11,6 +11,14 @@ Time Complexity of Solution: Best Case O(n); Average Case O(n); Worst Case O(n) */ + +/** + * bucketSort returns an array of numbers sorted in increasing order. + * + * @param {number[]} list The array of numbers to be sorted. + * @param {number} size The size of the buckets used. If not provided, size will be 5. + * @return {number[]} An array of numbers sorted in increasing order. +*/ function bucketSort (list, size) { if (undefined === size) { size = 5 From 877356fbab8c274ff6b2bd8a770d4fa7549105ae Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Mon, 4 Oct 2021 18:33:35 -0400 Subject: [PATCH 4/7] Fix bug in bucketSort so that a comparison function is now provided to the sort function The [built-in JavaScript sort method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) will perform a lexicographical (aka alphabetical) order by converting the elements of the array to strings. This can yield unexpected results when sorting numbers. A comparison function can be passed into the sort method to indicate how array elements should be compared. --- Sorts/BucketSort.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/BucketSort.js b/Sorts/BucketSort.js index 779cd2d62..d2cde4d2e 100644 --- a/Sorts/BucketSort.js +++ b/Sorts/BucketSort.js @@ -53,7 +53,7 @@ function bucketSort (list, size) { const sorted = [] // now sort every bucket and merge it to the sorted list for (let iBucket = 0; iBucket < buckets.length; iBucket++) { - const arr = buckets[iBucket].sort() + const arr = buckets[iBucket].sort((a, b) => a - b) for (let iSorted = 0; iSorted < arr.length; iSorted++) { sorted.push(arr[iSorted]) } From 1e3648af5ab54048538fadeb609bc6d27ff6a3dc Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Mon, 4 Oct 2021 18:35:00 -0400 Subject: [PATCH 5/7] Fix whitespace alignment --- Sorts/BucketSort.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/BucketSort.js b/Sorts/BucketSort.js index d2cde4d2e..84cc3feac 100644 --- a/Sorts/BucketSort.js +++ b/Sorts/BucketSort.js @@ -18,7 +18,7 @@ Best Case O(n); Average Case O(n); Worst Case O(n) * @param {number[]} list The array of numbers to be sorted. * @param {number} size The size of the buckets used. If not provided, size will be 5. * @return {number[]} An array of numbers sorted in increasing order. -*/ + */ function bucketSort (list, size) { if (undefined === size) { size = 5 From 11315ea449e8f66c323aaaa62ea1d6c468bda569 Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Mon, 4 Oct 2021 18:36:19 -0400 Subject: [PATCH 6/7] Add tests for BucketSort --- Sorts/test/BucketSort.test.js | 69 +++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Sorts/test/BucketSort.test.js diff --git a/Sorts/test/BucketSort.test.js b/Sorts/test/BucketSort.test.js new file mode 100644 index 000000000..d66027955 --- /dev/null +++ b/Sorts/test/BucketSort.test.js @@ -0,0 +1,69 @@ +import { bucketSort } from '../BucketSort' + +describe('Tests for bucketSort function', () => { + it('should correctly sort an input list that is sorted backwards', () => { + const array = [5, 4, 3, 2, 1] + expect(bucketSort(array)).toEqual([1, 2, 3, 4, 5]) + }) + + it('should correctly sort an input list that is unsorted', () => { + const array = [15, 24, 3, 2224, 1] + expect(bucketSort(array)).toEqual([1, 3, 15, 24, 2224]) + }) + + describe('Variations of input array lengths', () => { + it('should return an empty list with the input list is an empty list', () => { + expect(bucketSort([])).toEqual([]) + }) + + it('should correctly sort an input list of length 1', () => { + expect(bucketSort([100])).toEqual([100]) + }) + + it('should correctly sort an input list of an odd length', () => { + expect(bucketSort([101, -10, 321])).toEqual([-10, 101, 321]) + }) + + it('should correctly sort an input list of an even length', () => { + expect(bucketSort([40, 42, 56, 45, 12, 3])).toEqual([3, 12, 40, 42, 45, 56]) + }) + }) + + describe('Variations of input array elements', () => { + it('should correctly sort an input list that contains only positive numbers', () => { + expect(bucketSort([50, 33, 11, 2])).toEqual([2, 11, 33, 50]) + }) + + it('should correctly sort an input list that contains only negative numbers', () => { + expect(bucketSort([-1, -21, -2, -35])).toEqual([-35, -21, -2, -1]) + }) + + it('should correctly sort an input list that contains only a mix of positive and negative numbers', () => { + expect(bucketSort([-40, 42, 56, -45, 12, -3])).toEqual([-45, -40, -3, 12, 42, 56]) + }) + + it('should correctly sort an input list that contains only whole numbers', () => { + expect(bucketSort([11, 3, 12, 4, -15])).toEqual([-15, 3, 4, 11, 12]) + }) + + it('should correctly sort an input list that contains only decimal numbers', () => { + expect(bucketSort([1.0, 1.42, 2.56, 33.45, 13.12, 2.3])).toEqual([1.0, 1.42, 2.3, 2.56, 13.12, 33.45]) + }) + + it('should correctly sort an input list that contains only a mix of whole and decimal', () => { + expect(bucketSort([32.40, 12.42, 56, 45, 12, 3])).toEqual([3, 12, 12.42, 32.40, 45, 56]) + }) + + it('should correctly sort an input list that contains only fractional numbers', () => { + expect(bucketSort([0.98, 0.4259, 0.56, -0.456, -0.12, 0.322])).toEqual([-0.456, -0.12, 0.322, 0.4259, 0.56, 0.98]) + }) + + it('should correctly sort an input list that contains only a mix of whole, decimal, and fractional', () => { + expect(bucketSort([-40, -0.222, 5.6, -4.5, 12, 0.333])).toEqual([-40, -4.5, -0.222, 0.333, 5.6, 12]) + }) + + it('should correctly sort an input list that contains duplicates', () => { + expect(bucketSort([4, 3, 4, 2, 1, 2])).toEqual([1, 2, 2, 3, 4, 4]) + }) + }) +}) From 782386a11a61d819b3d24e41aea3157c1dc8fecb Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Tue, 5 Oct 2021 08:53:05 -0400 Subject: [PATCH 7/7] Remove testing code in function file --- Sorts/BucketSort.js | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/Sorts/BucketSort.js b/Sorts/BucketSort.js index 84cc3feac..b70bab2e9 100644 --- a/Sorts/BucketSort.js +++ b/Sorts/BucketSort.js @@ -61,14 +61,4 @@ function bucketSort (list, size) { return sorted } -// Testing -const arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14] -// > bucketSort(arrOrignal) -// [1, 2, 5, 6, 7, 8, 12, 14] -// Array before Sort -console.log(arrOrignal) -const arrSorted = bucketSort(arrOrignal) -// Array after sort -console.log(arrSorted) - export { bucketSort }