Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

bogo sort

They don't want me to tell you this, but this is actually the best sorting
algorithm*








*If you get the right input on your first try
Comment

bogo Sort

def bogosort(x):
    while np.any(x[:-1] > x[1:]):
        np.random.shuffle(x)
    return x
Comment

Bogo Sort

// Randomly shuffle the elements of the array that's passed in.
function shuffle(array) {
    var swapIndex = array.length;
    var temp, randomIndex;

    while (swapIndex !== 0) {

        randomIndex = Math.floor(Math.random() * swapIndex);

        swapIndex -= 1;

        temp = array[swapIndex];
        array[swapIndex] = array[randomIndex];
        array[randomIndex] = temp;
    }

    return array;
}

// Returns true if array is sorted, false if not.
function isSorted(array){
    for(var i = 1; i < array.length; i++) {
        if (array[i-1] > array[i]) {
            return false;
        }
    }
    return true;
}

// Shuffles array until it's sorted.
function bogoSort(array){
    while(isSorted(array) == false) {
        array = shuffle(array);
    }
    return array;
}

const testValues = [29, 100, 1, 2, 57];

var sorted = bogoSort(testValues);
console.log(sorted);
Comment

bogo Sort

x = np.array([2, 1, 4, 3, 5])
bogosort(x)
Comment

PREVIOUS NEXT
Code Example
Python :: slice in iloc 
Python :: how to access rows and columns indexing numpy 
Python :: <ipython-input-31-da456dc89cda in <module 
Python :: uneven chunks of array slices 
Python :: Drawing rectangle with border only in matplotlib 
Python :: pivot_table value aggfunct 
Python :: python can a imported module get variables from main module 
Python :: find no of iterations in python 
Python :: python3 main.py 
Python :: Ranking in Pyspark 
Python :: python pipe select where dedup 
Python :: filter pandas stack overflow 
Python :: how to save date in cookie Python 
Python :: Define a python function day_of_week, which displays the day name for a given date supplied in the form (day,month,year). 
Python :: comprehensive python cheat sheet 
Python :: python remove xa0 
Python :: how do i select a range of columns by index 
Python :: Python Record live streams (TS FILES) 
Python :: prime palindrome number in python 
Python :: how to reverse a number 
Python :: shape of a dataframe 
Python :: python polymorphism 
Python :: pandas add time to datetime 
Python :: sort 2d list python 
Python :: python < 
Python :: graph outlier detection 
Python :: semaphore in python 
Python :: python check for exception 
Python :: np.pad 
Python :: characters python 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =