Created
July 26, 2022 11:08
-
-
Save CyberGen49/a1fa05bfda24695ec8cf2e8e28ada86e to your computer and use it in GitHub Desktop.
An anti-sorting algorithm.
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
// Bozo Sort - An anti-sorting algorithm | |
function bozoSort(arr = []) { | |
for (let i = 0; i < arr.length; i++) { | |
let tmp = arr[i]; | |
let swapIndex = Math.round(Math.random()*(arr.length-1)); | |
arr[i] = arr[swapIndex]; | |
arr[swapIndex] = tmp; | |
} | |
return arr; | |
} | |
let array = [5, 9, 2, 3, 6, 4, 8, 7]; | |
console.log(array); | |
console.log(bozoSort(array)); // [8, 3, 7, 4, 5, 9, 6, 2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment