diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-bogosort.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-bogosort.md new file mode 100644 index 0000000000..95d0843d28 --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-bogosort.md @@ -0,0 +1,88 @@ +--- +id: 5a23c84252665b21eecc8002 +title: Sorting algorithms/Bogosort +challengeType: 5 +--- + +## Description +
+Bogosort a list of numbers. +Bogosort simply shuffles a collection randomly until it is sorted. +"Bogosort" is a perversely inefficient algorithm only used as an in-joke. +Its average run-time is O(n!) because the chance that any given shuffle of a set will end up in sorted order is about one in n factorial, and the worst case is infinite since there's no guarantee that a random shuffling will ever produce a sorted sequence. +Its best case is O(n) since a single pass through the elements may suffice to order them. +Pseudocode: +while not InOrder(list) do +Shuffle(list) +done +
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: bogosort should be a function. + testString: assert(typeof bogosort == 'function', 'bogosort should be a function.'); + - text: bogosort([25, 32, 12, 7, 20]) should return a array. + testString: assert(Array.isArray(bogosort([25, 32, 12, 7, 20])), 'bogosort([25, 32, 12, 7, 20]) should return a array.'); + - text: bogosort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32]. + testString: assert.deepEqual(bogosort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], 'bogosort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32].'); + - text: bogosort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45]. + testString: assert.deepEqual(bogosort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], 'bogosort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45].'); + - text: bogosort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43]. + testString: assert.deepEqual(bogosort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], 'bogosort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43].'); + - text: bogosort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38]. + testString: assert.deepEqual(bogosort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], 'bogosort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38].'); + - text: bogosort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48]. + testString: assert.deepEqual(bogosort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], 'bogosort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48].'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function bogosort (v) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function bogosort(v) { + function shuffle(v) { + for (var j, x, i = v.length; i; j = Math.floor(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x); + return v; + }; + + function isSorted(v) { + for (var i = 1; i < v.length; i++) { + if (v[i - 1] > v[i]) { + return false; + } + } + return true; + } + var sorted = false; + while (sorted == false) { + v = shuffle(v); + sorted = isSorted(v); + } + return v; +} +``` + +