-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathbogo-sort.js
48 lines (43 loc) · 1.01 KB
/
bogo-sort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Implement [Bogo sort]
*
* Copyright (c) 2018, Raphael Tomé Santana.
* Licensed under the MIT License.
*/
'use strict';
function shuffler(arr) {
for (let i = arr.length; i > 0;) {
const j = Math.floor(Math.random() * (--i));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}
function isSorted(arr) {
for (let i = 1; i < arr.length; i++) {
if (arr[i - 1] > arr[i]) return false;
}
return true;
}
/**
* Bogo sort algorithm.
* Complexity: O(Undefined).
*
* @example
* console.log(bogoSort([3, 1, 2])); // [ 1, 2, 3 ]
*
* @public
* @module sorting/bogosort
* @param {Array} array Input array.
* @param {Function} shuffle Optional. A function that defines an
* alternative shuffle order. The function should return a negative,
* zero, or positive value, depending on the arguments.
* @return {Array} Sorted array.
*/
function bogoSort(array, shuffle) {
shuffle = shuffle || shuffler;
while (!isSorted(array)) {
shuffler(array);
}
return array;
}
module.exports = bogoSort;