Skip to content

Instantly share code, notes, and snippets.

@CyberGen49
Created July 26, 2022 11:08
Show Gist options
  • Save CyberGen49/a1fa05bfda24695ec8cf2e8e28ada86e to your computer and use it in GitHub Desktop.
Save CyberGen49/a1fa05bfda24695ec8cf2e8e28ada86e to your computer and use it in GitHub Desktop.
An anti-sorting algorithm.
// 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