Last active
June 12, 2020 01:16
-
-
Save Alex4386/a1c3da181ae34ffbfd82144a4ee0e9bb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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