Search
 
SCRIPT & CODE EXAMPLE
 

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;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js wrap function 
Javascript :: yup.array not working 
Javascript :: onselect in zebra datepicker 
Javascript :: numbers split 2 
Javascript :: quasar router authentication 
Javascript :: object assign js 
Javascript :: javascript get string byte size 
Javascript :: angular autofocus 
Javascript :: axios.get Uncaught (in promise) TypeError: response.json is not a function 
Javascript :: react native updating options with setoptions 
Javascript :: get image from s3 bucket angular 
Javascript :: How to make PWAs installable js 
Javascript :: get all dates between two dates in moment js 
Javascript :: npm jwt decode 
Javascript :: * ws in ./node_modules/puppeteer/lib/WebSocketTransport.js 
Javascript :: adding data attributes to react-select 
Javascript :: vue.js props undefined type 
Javascript :: build json object 
Javascript :: Define Number Prop Vue 
Javascript :: remove shadow in jquery 
Javascript :: js array reduce 
Javascript :: jquery select element inside element 
Javascript :: download string as file express js 
Javascript :: delete an item from array javascript 
Javascript :: angular 12 features 
Javascript :: jsx full form 
Javascript :: contact form angular material 
Javascript :: foreach js 
Javascript :: open dev server 
Javascript :: how to global a variable in javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =