Skip to content

Instantly share code, notes, and snippets.

@JacksonGariety
Forked from 140bytes/LICENSE.txt
Last active January 1, 2016 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JacksonGariety/8168513 to your computer and use it in GitHub Desktop.
Save JacksonGariety/8168513 to your computer and use it in GitHub Desktop.

Tiny Bogo Sort in JavaScript

An implementation of the bogo sort algorithm in 130 bytes of JavaScript for the 140byt.es challenge.

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
}
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}
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.
{
"name": "tinyBogoSort",
"description": "JavaScript bogo sort in 139 bytes.",
"keywords": [
"bogo",
"sort"
"140byt.es",
"tiny"
]
}
<!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