From 9d12f76fd62c85152ec1802bd22e375ff85fea77 Mon Sep 17 00:00:00 2001 From: Evgenia Polozova Date: Sat, 3 Oct 2020 16:06:11 +0300 Subject: [PATCH 1/4] add BeadSort.js --- Sorts/BeadSort.js | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Sorts/BeadSort.js diff --git a/Sorts/BeadSort.js b/Sorts/BeadSort.js new file mode 100644 index 0000000000..9eac3aaf45 --- /dev/null +++ b/Sorts/BeadSort.js @@ -0,0 +1,66 @@ +/** + * Bead sort (also known as Gravity sort) + * https://en.wikipedia.org/wiki/Bead_sort + * + * Does counting sort of provided array according to + * the digit represented by exp. + * Only works for arrays of positive integers. + */ + +// > beadSort([-1, 5, 8, 4, 3, 19]) +// ! RangeError: Sequence must be a list of positive integers! +// > beadSort([5, 4, 3, 2, 1]) +// [1, 2, 3, 4, 5] +// > beadSort([7, 9, 4, 3, 5]) +// [3, 4, 5, 7, 9] + +function beadSort(sequence) { + // first, let's check that our sequence consists + // of positive integers + if (sequence.some((integer) => integer < 0)) { + throw RangeError("Sequence must be a list of positive integers!"); + } + + const sequenceLength = sequence.length; + const max = Math.max(...sequence); + + // set initial grid + let grid = sequence.map(number => { + let maxArr = new Array(max); + + for (let i = 0; i < number; i++) { + maxArr[i] = '*'; + } + + return maxArr; + }); + + // drop the beads! + for (let col = 0; col < max; col++) { + let beadsCount = 0; + + for (let row = 0, len = grid.length; row < grid.length; row++) { + if (grid[row][col] === '*') { + beadsCount++; + } + } + + for (let row = sequenceLength - 1; row > -1; row--) { + if (beadsCount) { + grid[row][col] = '*'; + beadsCount--; + } else { + grid[row][col] = undefined; + } + } + } + + // and, finally, let's turn our bead rows into their respective numbers + let sortedSequence = grid.map((beadArray) => { + let beadsArray = beadArray.filter(bead => bead === '*'); + + return beadsArray.length; + }); + + return sortedSequence; +} From 7b706e6b161568c926138c113f215cfbfd6434db Mon Sep 17 00:00:00 2001 From: Evgenia Polozova Date: Sat, 3 Oct 2020 16:11:36 +0300 Subject: [PATCH 2/4] add BeadSort to directory --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index dee2d724ce..32efef4451 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -99,6 +99,7 @@ * [LinearSearch](https://github.com/TheAlgorithms/Javascript/blob/master/Search/LinearSearch.js) ## Sorts + * [BeadSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/BeadSort.js) * [BogoSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/BogoSort.js) * [BubbleSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/BubbleSort.js) * [BucketSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/BucketSort.js) From 6814814c0425bf5860ca04a5b42072880abb0058 Mon Sep 17 00:00:00 2001 From: Evgenia Polozova Date: Sat, 3 Oct 2020 17:58:28 +0300 Subject: [PATCH 3/4] run npx standard & fix linter issues --- Sorts/BeadSort.js | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/Sorts/BeadSort.js b/Sorts/BeadSort.js index 9eac3aaf45..92fe234d28 100644 --- a/Sorts/BeadSort.js +++ b/Sorts/BeadSort.js @@ -18,49 +18,49 @@ function beadSort(sequence) { // first, let's check that our sequence consists // of positive integers if (sequence.some((integer) => integer < 0)) { - throw RangeError("Sequence must be a list of positive integers!"); + throw RangeError('Sequence must be a list of positive integers!') } - const sequenceLength = sequence.length; - const max = Math.max(...sequence); + const sequenceLength = sequence.length + const max = Math.max(...sequence) // set initial grid - let grid = sequence.map(number => { - let maxArr = new Array(max); + const grid = sequence.map(number => { + const maxArr = new Array(max) for (let i = 0; i < number; i++) { - maxArr[i] = '*'; + maxArr[i] = '*' } - return maxArr; - }); + return maxArr + }) // drop the beads! for (let col = 0; col < max; col++) { - let beadsCount = 0; + let beadsCount = 0 - for (let row = 0, len = grid.length; row < grid.length; row++) { + for (let row = 0; row < sequenceLength; row++) { if (grid[row][col] === '*') { - beadsCount++; + beadsCount++ } } for (let row = sequenceLength - 1; row > -1; row--) { if (beadsCount) { - grid[row][col] = '*'; - beadsCount--; + grid[row][col] = '*' + beadsCount-- } else { - grid[row][col] = undefined; + grid[row][col] = undefined } } } // and, finally, let's turn our bead rows into their respective numbers - let sortedSequence = grid.map((beadArray) => { - let beadsArray = beadArray.filter(bead => bead === '*'); + const sortedSequence = grid.map((beadArray) => { + const beadsArray = beadArray.filter(bead => bead === '*') - return beadsArray.length; - }); + return beadsArray.length + }) - return sortedSequence; + return sortedSequence } From 944cdc4707a8e88a2302929478293c51dd4469d8 Mon Sep 17 00:00:00 2001 From: Evgenia Polozova Date: Sat, 3 Oct 2020 18:02:33 +0300 Subject: [PATCH 4/4] add bead sort implementation w/ console.log() --- Sorts/BeadSort.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sorts/BeadSort.js b/Sorts/BeadSort.js index 92fe234d28..1261fd554b 100644 --- a/Sorts/BeadSort.js +++ b/Sorts/BeadSort.js @@ -14,7 +14,7 @@ // > beadSort([7, 9, 4, 3, 5]) // [3, 4, 5, 7, 9] -function beadSort(sequence) { +function beadSort (sequence) { // first, let's check that our sequence consists // of positive integers if (sequence.some((integer) => integer < 0)) { @@ -64,3 +64,6 @@ function beadSort(sequence) { return sortedSequence } + +// implementation +console.log(beadSort([5, 4, 3, 2, 1]))