Simple implementation of Bogobogosort
NOTE: For obvious reasons, there isn't a clear document describing how Bogobogosort works. This implementation includes both popular versions with the default being the slower one.
This version will probably continue sorting until the heat-death of the universe on any sizable list. It has an average complexity of O(N!1!2!3!...N!) and a worst case of O(∞).
- Get first
n
(n
starts at 2) items of list. - Shuffle
n
items (uses Fisher-Yates shuffle). - If
n
items are not sorted, setn
back to 2. If they are sorted, thenn
increases by one unlessn
already includes the whole array, in which case, the sort is finished. - Return to step 1.
This version is much faster as it will attempt to sort an array segment ∞ times instead of 1. This still yields a worst case of O(∞) but an average complexity which isn't as cool.
- Get first
n
(n
starts at 2) items of list. - Shuffle
n
items (uses Fisher-Yates shuffle). - If
n
items are not sorted, return to step 2. - If
n
includes who array then sort is finished, if not, increasen
by one and return to step 1.
$ npm install --save bogobogo
Warning: The tests could theoretically run forever. They likely won't, but it could happen.
$ npm test
var bogobogo = require('bogobogo');
bogobogo.create([66, 22, 3, 5, 1, 19]).then(function(result){
console.log(result);
}).standard();
bogobogo.create([6, 111, 44, 1, 5, 23]).then(function(result){
console.log(result);
}).fast();
MIT © Falkirks