An implementation of the bogo sort algorithm in 130 bytes of JavaScript for the 140byt.es challenge.
-
-
Save ColemanGariety/8168513 to your computer and use it in GitHub Desktop.
This file contains 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 (a, b) { // 'a' is our array, 'b' is wether or not it is sorted | |
while (!b) { // Loop until our array is sorted | |
a = a.sort(function () { // Sort our array each time we loop... | |
return .5 - Math.random() // ...with a simple shuffle method | |
}); | |
// Now we check if our array is sorted | |
for (i = a.length; i--;) { // Loop over the array | |
if (a[i - 1] > a[i]) { // If the next item is ever greater than the current one... | |
break // ...then our array isn't sorted yet, so break out of the loop | |
} | |
} | |
i < 0 ? b = 1 : 0 // The 'i < 0' part checks if the loop completed without 'break'-ing | |
// If the loop completed this time, that means our array is sorted... | |
// ...so set 'b' to a truthy value so the loop on line #3 ends | |
} | |
return a // Return our array | |
} |
This file contains 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(a,b){while(!b){a=a.sort(function(){return.5-Math.random()});for(i=a.length;i--;){if(a[i-1]>a[i])break}i<0?b=1:0}return a} |
This file contains 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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Jackson Gariety http://jacksongariety.com/ | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains 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
{ | |
"name": "tinyBogoSort", | |
"description": "JavaScript bogo sort in 139 bytes.", | |
"keywords": [ | |
"bogo", | |
"sort" | |
"140byt.es", | |
"tiny" | |
] | |
} |
This file contains 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
<!DOCTYPE html> | |
<title>Tiny Bogo Sort Test</title> | |
<div>Expected value: <b>[1,2,3,4,5]</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
var unsorted = [1, 5, 4, 2, 3] | |
var tinyBogoSort = function(a,b){while(!b){a=a.sort(function(){return.5-Math.random()});for(i=a.length;i--;){if(a[i-1]>a[i])break}i<0?b=1:0}return a} | |
document.getElementById( "ret" ).innerHTML = '[' + tinyBogoSort(unsorted) + ']' | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment