This Gist was automatically created by Carbide, a free online programming environment.
Last active
May 6, 2021 09:36
-
-
Save carbide-public/80622da385315ddf93ba7d76613d6148 to your computer and use it in GitHub Desktop.
untitled
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
const sortShuffle = (arr) => arr.slice().sort(() => 0.5 - Math.random()) | |
function durstenfeldShuffle(array) { | |
let arr = array.slice(); | |
for (let i = arr.length - 1; i > 0; i--) { | |
let r = Math.floor(Math.random() * (i + 1)); | |
[arr[i], arr[r]] = [arr[r], arr[i]]; | |
} | |
return arr | |
} | |
let sortErr = 0 | |
let durErr = 0 | |
let array = [1,2,3,4,5,6,7] | |
let sort_shuffled_array = sortShuffle(array) | |
sortErr = sortErr + (sort_shuffled_array[0] === array[0]) ? 1 : 0 | |
let durstenfeld_shuffled_array = durstenfeldShuffle(array) | |
durErr += durstenfeld_shuffled_array[0] === array[0] ? 1 : 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment