Created
October 22, 2017 10:12
-
-
Save loke-dev/4457913f49eb2d314a6f3f797af7740a to your computer and use it in GitHub Desktop.
Bogosort benchamark
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
var a = randomArray(11, 11); | |
function randomArray(length, max) { | |
return Array.apply(null, Array(length)).map(function() { | |
return Math.round(Math.random() * max); | |
}); | |
} | |
shuffle = function(v) { | |
for(var j, x, i = v.length; i; j = Math.floor(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x); | |
return v; | |
}; | |
isSorted = function(v){ | |
for(var i=1; i<v.length; i++) { | |
if (v[i-1] > v[i]) { return false; } | |
} | |
return true; | |
} | |
bogosort = function(v){ | |
var sorted = false; | |
while(sorted == false){ | |
v = shuffle(v); | |
sorted = isSorted(v); | |
} | |
return v; | |
} | |
var start = new Date().getTime(); | |
bogosort(a) | |
var end = new Date().getTime(); | |
var time = end - start; | |
console.log("Done") | |
console.log("Sorted an array with " + a.length + " elements") | |
console.log("It took " + time + " ms to sort!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment