|
1 | 1 | /**
|
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. |
| 2 | + * Bead Sort, also known as Gravity sort. |
| 3 | + * |
| 4 | + * This algorithm was inspired from natural phenomena and was designed keeping in mind objects (or beads) falling under |
| 5 | + * the influence of gravity. |
5 | 6 | *
|
6 | 7 | * NOTE: It only works for arrays of positive integers.
|
7 | 8 | *
|
8 | 9 | * Wikipedia: https://en.wikipedia.org/wiki/Bead_sort
|
9 | 10 | */
|
10 |
| - |
11 |
| -/** |
12 |
| - * Doctests |
13 |
| - * |
14 |
| - * > beadSort([5, 4, 3, 2, 1]) |
15 |
| - * [1, 2, 3, 4, 5] |
16 |
| - * > beadSort([7, 9, 4, 3, 5]) |
17 |
| - * [3, 4, 5, 7, 9] |
18 |
| - * > beadSort([-1, 5, 8, 4, 3, 19]) |
19 |
| - * ! RangeError: Sequence must be a list of positive integers! |
20 |
| - */ |
21 |
| - |
22 |
| -function beadSort (sequence) { |
| 11 | +export function beadSort (sequence) { |
23 | 12 | /* Let's ensure our sequence has only Positive Integers */
|
24 | 13 | if (sequence.some((integer) => integer < 0)) {
|
25 | 14 | throw RangeError('Sequence must be a list of Positive integers Only!')
|
@@ -60,23 +49,8 @@ function beadSort (sequence) {
|
60 | 49 | }
|
61 | 50 |
|
62 | 51 | /* Finally, let's turn our Bead rows into their Respective Numbers */
|
63 |
| - const sortedSequence = grid.map((beadArray) => { |
| 52 | + return grid.map((beadArray) => { |
64 | 53 | const beadsArray = beadArray.filter(bead => bead === '*')
|
65 |
| - |
66 | 54 | return beadsArray.length
|
67 | 55 | })
|
68 |
| - |
69 |
| - return sortedSequence |
70 | 56 | }
|
71 |
| - |
72 |
| -/** |
73 |
| -* Implementation of Bead Sort |
74 |
| -*/ |
75 |
| -const array = [5, 4, 3, 2, 1] |
76 |
| -// Before Sort |
77 |
| -console.log('\n- Before Sort | Implementation of Bead Sort -') |
78 |
| -console.log(array) |
79 |
| -// After Sort |
80 |
| -console.log('- After Sort | Implementation of Bead Sort -') |
81 |
| -console.log(beadSort(array)) |
82 |
| -console.log('\n') |
0 commit comments