11/**
2- * Bead sort (also known as Gravity sort)
3- * https://en.wikipedia.org/wiki/Bead_sort
2+ * Bead Sort, also known as Gravity sort, this algorithm was
3+ * inspired from natural phenomenons and was designed keeping in mind objects(or beads)
4+ * falling under the influence of gravity.
45 *
5- * Does counting sort of provided array according to
6- * the digit represented by exp.
7- * Only works for arrays of positive integers.
6+ * NOTE: It only works for arrays of positive integers.
7+ *
8+ * Wikipedia: https://en.wikipedia.org/wiki/Bead_sort
89 */
910
1011/**
1112 * Doctests
1213 *
13- * > beadSort([-1, 5, 8, 4, 3, 19])
14- * ! RangeError: Sequence must be a list of positive integers!
1514 * > beadSort([5, 4, 3, 2, 1])
1615 * [1, 2, 3, 4, 5]
1716 * > beadSort([7, 9, 4, 3, 5])
1817 * [3, 4, 5, 7, 9]
18+ * > beadSort([-1, 5, 8, 4, 3, 19])
19+ * ! RangeError: Sequence must be a list of positive integers!
1920 */
2021
2122function beadSort ( sequence ) {
22- // first, let's check that our sequence consists
23- // of positive integers
23+ /* Let's ensure our sequence has only Positive Integers */
2424 if ( sequence . some ( ( integer ) => integer < 0 ) ) {
25- throw RangeError ( 'Sequence must be a list of positive integers!' )
25+ throw RangeError ( 'Sequence must be a list of Positive integers Only !' )
2626 }
2727
2828 const sequenceLength = sequence . length
2929 const max = Math . max ( ...sequence )
3030
31- // set initial grid
31+ // Set initial Grid
3232 const grid = sequence . map ( number => {
3333 const maxArr = new Array ( max )
3434
@@ -39,7 +39,7 @@ function beadSort (sequence) {
3939 return maxArr
4040 } )
4141
42- // drop the beads !
42+ // Drop the Beads !
4343 for ( let col = 0 ; col < max ; col ++ ) {
4444 let beadsCount = 0
4545
@@ -59,7 +59,7 @@ function beadSort (sequence) {
5959 }
6060 }
6161
62- // and, finally, let's turn our bead rows into their respective numbers
62+ /* Finally, let's turn our Bead rows into their Respective Numbers */
6363 const sortedSequence = grid . map ( ( beadArray ) => {
6464 const beadsArray = beadArray . filter ( bead => bead === '*' )
6565
@@ -70,7 +70,7 @@ function beadSort (sequence) {
7070}
7171
7272/**
73- * Implementation of Cocktail Shaker Sort
73+ * Implementation of Bead Sort
7474*/
7575const array = [ 5 , 4 , 3 , 2 , 1 ]
7676// Before Sort
0 commit comments