1
1
/**
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.
4
5
*
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
8
9
*/
9
10
10
11
/**
11
12
* Doctests
12
13
*
13
- * > beadSort([-1, 5, 8, 4, 3, 19])
14
- * ! RangeError: Sequence must be a list of positive integers!
15
14
* > beadSort([5, 4, 3, 2, 1])
16
15
* [1, 2, 3, 4, 5]
17
16
* > beadSort([7, 9, 4, 3, 5])
18
17
* [3, 4, 5, 7, 9]
18
+ * > beadSort([-1, 5, 8, 4, 3, 19])
19
+ * ! RangeError: Sequence must be a list of positive integers!
19
20
*/
20
21
21
22
function 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 */
24
24
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 !' )
26
26
}
27
27
28
28
const sequenceLength = sequence . length
29
29
const max = Math . max ( ...sequence )
30
30
31
- // set initial grid
31
+ // Set initial Grid
32
32
const grid = sequence . map ( number => {
33
33
const maxArr = new Array ( max )
34
34
@@ -39,7 +39,7 @@ function beadSort (sequence) {
39
39
return maxArr
40
40
} )
41
41
42
- // drop the beads !
42
+ // Drop the Beads !
43
43
for ( let col = 0 ; col < max ; col ++ ) {
44
44
let beadsCount = 0
45
45
@@ -59,7 +59,7 @@ function beadSort (sequence) {
59
59
}
60
60
}
61
61
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 */
63
63
const sortedSequence = grid . map ( ( beadArray ) => {
64
64
const beadsArray = beadArray . filter ( bead => bead === '*' )
65
65
@@ -70,7 +70,7 @@ function beadSort (sequence) {
70
70
}
71
71
72
72
/**
73
- * Implementation of Cocktail Shaker Sort
73
+ * Implementation of Bead Sort
74
74
*/
75
75
const array = [ 5 , 4 , 3 , 2 , 1 ]
76
76
// Before Sort
0 commit comments