Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

bogo sort js

// Bogosort is the worst sorting algorithm 
// It has a time range of 0 to Infinity
// Here it is!

function isSorted(arr) {
    for(var i = 1; i < arr.length; i++){
        if (arr[i-1] > arr[i]) {
            return false;
        }
    }
    return true;
}

function shuffle(array) {
  let currentIndex = array.length,  randomIndex;
  while (currentIndex != 0) {
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex--;

    [array[currentIndex], array[randomIndex]] = [
      array[randomIndex], array[currentIndex]];
  }

  return array;
}

function bogoSort(arr) {
  while (!isSorted(arr)) {
  	arr = shuffle(arr);
  }
  return arr;
}
 
PREVIOUS NEXT
Tagged: #bogo #sort #js
ADD COMMENT
Topic
Name
1+5 =