Skip to content

Instantly share code, notes, and snippets.

@Alex4386
Last active June 12, 2020 01:16
Show Gist options
  • Save Alex4386/a1c3da181ae34ffbfd82144a4ee0e9bb to your computer and use it in GitHub Desktop.
Save Alex4386/a1c3da181ae34ffbfd82144a4ee0e9bb to your computer and use it in GitHub Desktop.
function isSorted(a) {
for (let i = 0; i < a.length - 1; i++) {
if (a[i] > a[i+1]) return false;
}
return true;
}
function bogoSort(a) {
while (!isSorted(a)) {
a.sort(() => Math.random() - 0.5);
}
return a;
}
function isSorted(a) {
for (let i = 0; i < a.length - 1; i++) {
if (a[i] > a[i+1]) return false;
}
return true;
}
function bogoSort(a) {
a.sort(() => Math.random() - 0.5);
return isSorted(a) ? a : bogoSort(a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment