diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-bead-sort.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-bead-sort.md new file mode 100644 index 0000000000..45aa965d86 --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-bead-sort.md @@ -0,0 +1,97 @@ +--- +id: 5a23c84252665b21eecc8001 +title: Sorting algorithms/Bead sort +challengeType: 5 +--- + +## Description +
+Sort an array of positive integers using the Bead Sort Algorithm. +A bead sort is also known as a gravity sort. +Algorithm has O(S), where S is the sum of the integers in the input set: Each bead is moved individually. +This is the case when bead sort is implemented without a mechanism to assist in finding empty spaces below the beads, such as in software implementations. +
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: beadSort should be a function. + testString: assert(typeof beadSort == 'function', 'beadSort should be a function.'); + - text: beadSort([25, 32, 12, 7, 20]) should return a array. + testString: assert(Array.isArray(beadSort([25, 32, 12, 7, 20])), 'beadSort([25, 32, 12, 7, 20]) should return a array.'); + - text: beadSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32]. + testString: assert.deepEqual(beadSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], 'beadSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32].'); + - text: beadSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45]. + testString: assert.deepEqual(beadSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], 'beadSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45].'); + - text: beadSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43]. + testString: assert.deepEqual(beadSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], 'beadSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43].'); + - text: beadSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38]. + testString: assert.deepEqual(beadSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], 'beadSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38].'); + - text: beadSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48]. + testString: assert.deepEqual(beadSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], 'beadSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48].'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function beadSort (arr) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function beadSort(arr) { + var max = 0; + for (var i = 0; i < arr.length; i++) + if (arr[i] > max) + max = arr[i];; + var grid = new Array(arr.length); + for (var i = 0; i < grid.length; i++) { + grid[i] = new Array(max); + } + var levelcount = new Array(max); + levelcount.fill(0) + for (var i = 0; i < max; i++) { + levelcount[i] = 0; + for (var j = 0; j < arr.length; j++) + grid[j][i] = '_'; + }; + for (var i = 0; i < arr.length; i++) { + var num = arr[i]; + for (var j = 0; num > 0; j++) { + grid[levelcount[j]++][j] = '*'; + num--; + }; + }; + var sorted = new Array(arr.length) + sorted.fill(0) + for (var i = 0; i < arr.length; i++) { + var putt = 0; + for (var j = 0; j < max && (function (c) { + return c.charCodeAt == null ? c : c.charCodeAt(0); + })(grid[arr.length - 1 - i][j]) == '*'.charCodeAt(0); j++) + putt++; + sorted[i] = putt; + }; + return sorted; +} +``` + +