From f1c261695aeb7f29844097276bb26c0348624381 Mon Sep 17 00:00:00 2001 From: Cfaust Date: Fri, 9 Aug 2019 10:06:44 -0600 Subject: [PATCH 1/3] Bucket Sort Not perfect but a good start --- .../sorting/Bucket-sort/BucketSort.js | 39 +++++++++++ src/algorithms/sorting/Bucket-sort/README.md | 42 ++++++++++++ .../Bucket-sort/__test__/BucketSort.test.js | 64 +++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 src/algorithms/sorting/Bucket-sort/BucketSort.js create mode 100644 src/algorithms/sorting/Bucket-sort/README.md create mode 100644 src/algorithms/sorting/Bucket-sort/__test__/BucketSort.test.js diff --git a/src/algorithms/sorting/Bucket-sort/BucketSort.js b/src/algorithms/sorting/Bucket-sort/BucketSort.js new file mode 100644 index 00000000..601fd599 --- /dev/null +++ b/src/algorithms/sorting/Bucket-sort/BucketSort.js @@ -0,0 +1,39 @@ +import Sort from '../Sort'; + +export default class BucketSort extends Sort { + sort(originalArray) { + let array = []; + let bucketSize = originalArray.length; + + let minValue = originalArray[0]; + let maxValue = originalArray[0]; + originalArray.forEach(function (currentVal) { //get Min and Max number in array + if (currentVal < minValue) { + minValue = currentVal; + } else if (currentVal > maxValue) { + maxValue = currentVal; + } + }) + + let buckets = new Array(bucketSize); + + for (var i = 0; i < buckets.length; i++) { //make 2d array for storing numbers in different buckets + buckets[i] = new Array(bucketSize); + } + + originalArray.forEach(element => { //for each item in array sort them into their respected bucket + let index = Math.floor(element - minValue); // get index by getting the number - min value in array. + buckets[index].push(element); //pushes element into bucket of index we found + }); + + buckets.forEach(element => { // Concat all buckets into one array + element.forEach(number => { + if (typeof number !== 'undefined') { + array.push(number); + } + }); + }); + + return array + } +} \ No newline at end of file diff --git a/src/algorithms/sorting/Bucket-sort/README.md b/src/algorithms/sorting/Bucket-sort/README.md new file mode 100644 index 00000000..c87f75aa --- /dev/null +++ b/src/algorithms/sorting/Bucket-sort/README.md @@ -0,0 +1,42 @@ +# Bucket Sort + +**Bucket sort** is a sorting algorithm that works by distributing +elements of an array into a number of different buckets.Each bucket +representing a number range, then the buckets are sorted individually, +either using a different sorting algorithm, or by recursively applying +the bucket sorting algorithm. + +The average time complexity for Bucket Sort is O(n + k) (k = number of buckets). The worst time +complexity is O(n²). + +## Algorithm + +**Step I** + +Our first real step is to create our buckets, we make an array thats the size of our original array, and then that array into a 2D array that will represent our buckets. This array will hold all of our elements for when we sort them. + + + +**Step II** + +Next step we do the sorting, we find the index of the bucket we want to add our value to by doing a small equation. We take the element we want to sort and subtract the smallest value from our original array, and thats our index, so we just push that to our buckets array. + + + +**Step III** + + + + + +## Complexity + +| Name | Best | Average | Worst | Memory | Stable | Comments | +| --------------------- | :-------------: | :-----------------: | :-----------------: | :-------: | :-------: | :-------- | +| **Counting sort** | n + r | n + r | n + r | n + r | Yes | r - biggest number in array | + +## References + +- [Wikipedia](https://en.wikipedia.org/wiki/Bucket_sort) +- [YouTube](https://www.youtube.com/watch?v=VuXbEb5ywrU) +- [GeeksforGeeks](https://www.geeksforgeeks.org/bucket-sort-2/) diff --git a/src/algorithms/sorting/Bucket-sort/__test__/BucketSort.test.js b/src/algorithms/sorting/Bucket-sort/__test__/BucketSort.test.js new file mode 100644 index 00000000..42c93ca2 --- /dev/null +++ b/src/algorithms/sorting/Bucket-sort/__test__/BucketSort.test.js @@ -0,0 +1,64 @@ +import BucketSort from '../BucketSort'; +import { + equalArr, + notSortedArr, + reverseArr, + sortedArr, + SortTester, +} from '../../SortTester'; + +// Complexity constants. +// const SORTED_ARRAY_VISITING_COUNT = 20; +// const NOT_SORTED_ARRAY_VISITING_COUNT = 189; +// const REVERSE_SORTED_ARRAY_VISITING_COUNT = 209; +// const EQUAL_ARRAY_VISITING_COUNT = 20; + +describe('BucketSort', () => { + it('should sort array', () => { + SortTester.testSort(BucketSort); + }); + + // it('should sort array with custom comparator', () => { + // SortTester.testSortWithCustomComparator(BucketSort); + // }); + + // it('should do stable sorting', () => { + // SortTester.testSortStability(BucketSort); + // }); + + // it('should sort negative numbers', () => { + // SortTester.testNegativeNumbersSort(BucketSort); + // }); + + // it('should visit EQUAL array element specified number of times', () => { + // SortTester.testAlgorithmTimeComplexity( + // BucketSort, + // equalArr, + // EQUAL_ARRAY_VISITING_COUNT, + // ); + // }); + + // it('should visit SORTED array element specified number of times', () => { + // SortTester.testAlgorithmTimeComplexity( + // BucketSort, + // sortedArr, + // SORTED_ARRAY_VISITING_COUNT, + // ); + // }); + + // it('should visit NOT SORTED array element specified number of times', () => { + // SortTester.testAlgorithmTimeComplexity( + // BucketSort, + // notSortedArr, + // NOT_SORTED_ARRAY_VISITING_COUNT, + // ); + // }); + + // it('should visit REVERSE SORTED array element specified number of times', () => { + // SortTester.testAlgorithmTimeComplexity( + // BucketSort, + // reverseArr, + // REVERSE_SORTED_ARRAY_VISITING_COUNT, + // ); + // }); +}); From 80e4d05ae1a83a84771ab4270637aebfbdd1ae12 Mon Sep 17 00:00:00 2001 From: Cfaust Date: Fri, 9 Aug 2019 10:09:44 -0600 Subject: [PATCH 2/3] updated read me --- src/algorithms/sorting/Bucket-sort/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/algorithms/sorting/Bucket-sort/README.md b/src/algorithms/sorting/Bucket-sort/README.md index c87f75aa..f48a03ee 100644 --- a/src/algorithms/sorting/Bucket-sort/README.md +++ b/src/algorithms/sorting/Bucket-sort/README.md @@ -25,7 +25,7 @@ Next step we do the sorting, we find the index of the bucket we want to add our **Step III** - +Our last and final step is to compine all of our buckets to one array so we can return the sorted array. @@ -33,7 +33,7 @@ Next step we do the sorting, we find the index of the bucket we want to add our | Name | Best | Average | Worst | Memory | Stable | Comments | | --------------------- | :-------------: | :-----------------: | :-----------------: | :-------: | :-------: | :-------- | -| **Counting sort** | n + r | n + r | n + r | n + r | Yes | r - biggest number in array | +| **Counting sort** | n | n + k | n^{2} | n + k | Yes | k - number of buckets | ## References From 1fc9f6447e405440f01bd5dd3fe24e2a68e0d575 Mon Sep 17 00:00:00 2001 From: Cfaust Date: Fri, 9 Aug 2019 10:10:18 -0600 Subject: [PATCH 3/3] read me --- src/algorithms/sorting/Bucket-sort/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/sorting/Bucket-sort/README.md b/src/algorithms/sorting/Bucket-sort/README.md index f48a03ee..69088ef9 100644 --- a/src/algorithms/sorting/Bucket-sort/README.md +++ b/src/algorithms/sorting/Bucket-sort/README.md @@ -33,7 +33,7 @@ Our last and final step is to compine all of our buckets to one array so we can | Name | Best | Average | Worst | Memory | Stable | Comments | | --------------------- | :-------------: | :-----------------: | :-----------------: | :-------: | :-------: | :-------- | -| **Counting sort** | n | n + k | n^{2} | n + k | Yes | k - number of buckets | +| **Counting sort** | n | n + k | n^2 | n + k | Yes | k - number of buckets | ## References