From c67a570c456427cf487cab37fefbeb6b045e45d9 Mon Sep 17 00:00:00 2001 From: marsonya Date: Tue, 22 Dec 2020 23:58:40 +0530 Subject: [PATCH 1/3] improved doctests presentation --- Sorts/BeadSort.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Sorts/BeadSort.js b/Sorts/BeadSort.js index 1261fd554..518e539d1 100644 --- a/Sorts/BeadSort.js +++ b/Sorts/BeadSort.js @@ -7,12 +7,16 @@ * 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] +/** + * Doctests + * + * > 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 From a3e6c4c69e0c380f5fdb9351f2f506f496c3249f Mon Sep 17 00:00:00 2001 From: marsonya Date: Fri, 25 Dec 2020 23:57:21 +0530 Subject: [PATCH 2/3] formatted implementation --- Sorts/BeadSort.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Sorts/BeadSort.js b/Sorts/BeadSort.js index 518e539d1..ed88ca705 100644 --- a/Sorts/BeadSort.js +++ b/Sorts/BeadSort.js @@ -69,5 +69,14 @@ function beadSort (sequence) { return sortedSequence } -// implementation -console.log(beadSort([5, 4, 3, 2, 1])) +/** +* Implementation of Cocktail Shaker Sort +*/ +const array = [5, 4, 3, 2, 1] +// Before Sort +console.log('\n- Before Sort | Implementation of Bead Sort -') +console.log(array) +// After Sort +console.log('- After Sort | Implementation of Bead Sort -') +console.log(beadSort(array)) +console.log('\n') From 2acff23fdd25c60f1c38749b52f51cd9b7ec1e1c Mon Sep 17 00:00:00 2001 From: marsonya Date: Wed, 30 Dec 2020 11:01:24 +0530 Subject: [PATCH 3/3] improved comments and algo description --- Sorts/BeadSort.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Sorts/BeadSort.js b/Sorts/BeadSort.js index ed88ca705..7256a1a92 100644 --- a/Sorts/BeadSort.js +++ b/Sorts/BeadSort.js @@ -1,34 +1,34 @@ /** - * Bead sort (also known as Gravity sort) - * https://en.wikipedia.org/wiki/Bead_sort + * Bead Sort, also known as Gravity sort, this algorithm was + * inspired from natural phenomenons and was designed keeping in mind objects(or beads) + * falling under the influence of gravity. * - * Does counting sort of provided array according to - * the digit represented by exp. - * Only works for arrays of positive integers. + * NOTE: It only works for arrays of positive integers. + * + * Wikipedia: https://en.wikipedia.org/wiki/Bead_sort */ /** * Doctests * - * > 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] + * > beadSort([-1, 5, 8, 4, 3, 19]) + * ! RangeError: Sequence must be a list of positive integers! */ function beadSort (sequence) { - // first, let's check that our sequence consists - // of positive integers + /* Let's ensure our sequence has only 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 Only!') } const sequenceLength = sequence.length const max = Math.max(...sequence) - // set initial grid + // Set initial Grid const grid = sequence.map(number => { const maxArr = new Array(max) @@ -39,7 +39,7 @@ function beadSort (sequence) { return maxArr }) - // drop the beads! + // Drop the Beads! for (let col = 0; col < max; col++) { let beadsCount = 0 @@ -59,7 +59,7 @@ function beadSort (sequence) { } } - // and, finally, let's turn our bead rows into their respective numbers + /* Finally, let's turn our Bead rows into their Respective Numbers */ const sortedSequence = grid.map((beadArray) => { const beadsArray = beadArray.filter(bead => bead === '*') @@ -70,7 +70,7 @@ function beadSort (sequence) { } /** -* Implementation of Cocktail Shaker Sort +* Implementation of Bead Sort */ const array = [5, 4, 3, 2, 1] // Before Sort