GO-V.ORG
As seen in

Bogosort

In computer science, bogosort (also known as permutation sort and stupid sort ) is a sorting algorithm based on the generate and test paradigm. The function successively generates permutations of its input until it finds one that is sorted. It is not considered useful for sorting, but may be used for educational purposes, to contrast it with more efficient algorithms. The algorithm's name is a portmanteau of the words bogus and sort. Two versions of this algorithm exist: a deterministic version that enumerates all permutations until it hits a sorted one, and a randomized version that randomly permutes its input and checks whether it is sorted. An analogy for the working of the latter... Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.

In computer science, bogosort (also known as permutation sort and stupid sort ) is a sorting algorithm based on the generate and test paradigm. The function successively generates permutations of its input until it finds one that is sorted. It is not considered useful for sorting, but may be used for educational purposes, to contrast it with more efficient algorithms. The algorithm's name is a portmanteau of the words bogus and sort. Two versions of this algorithm exist: a deterministic version that enumerates all permutations until it hits a sorted one, and a randomized version that randomly permutes its input and checks whether it is sorted. An analogy for the working of the latter version is to sort a deck of cards by throwing the deck into the air, picking the cards up at random, and repeating the process until the deck is sorted. In a worst-case scenario with this version, the random source is of low quality and happens to make the sorted permutation unlikely to occur. P(\text{at least one success}) = 1 - (P(\text{failure in one trial}))^n Here, n represents the number of independent shuffles (trials). In the context of Bogosort, a "success" corresponds to producing a sorted permutation, while a "failure" corresponds to producing an unsorted permutation. Since only one of the n! possible permutations is sorted, the probability of success in a single trial is 1/n! . Thus, the probability of obtaining a sorted list within k shuffles is: P(\text{sorted within } k \text{ shuffles}) = 1 - \left(1 - \tfrac{1}{n!}\right)^k This framing highlights the extreme inefficiency of Bogosort: even for modest values of n , the probability of success remains vanishingly small unless k is astronomically large. ==Description of the algorithm== include include include include include void shuffle(int a[], int length) { int temp; int random; for (size_t i = 0; i a[i + 1]) { return false; } } return true; } void bogoSort(int a[], int length) { while (!sorted(a, length)) { shuffle(a, length); } } int main(void) { int input[] = {68, 14, 78, 98, 67, 89, 45, 90, 87, 78, 65, 74}; int size = sizeof(input) / sizeof(input[0]); srand((unsigned)time(NULL)); bogoSort(input, size); printf("Sorted result:"); for (size_t i = 0; i import random this function checks whether or not the array is sorted def is_sorted(a: list[int]) -> bool: for i in range(1, len(a)): if a[i] list[int]: while not is_sorted(a): random.shuffle(a) return a this function generates an array with randomly chosen integer values def generate_random_array(size: int, min_val: int, max_val: int) -> list[int]: return [random.randint(min_val, max_val) for _ in range(size)] if __name__ == "__main__": # the size, minimum value and maximum value of the randomly generated array size: int = 10 min_val: int = 1 max_val: int = 100 random_array: list[int] = generate_random_array(size, min_val, max_val) print("Unsorted array:", random_array) sorted_arr = bogo_sort(random_array) print("Sorted array:", sorted_arr) This code creates a random array - random_array - in generate_random_array that would be sorted by shuffling it in bogosort. All data in the array is natural numbers from 1 - 100. ==Running time and termination== If all elements to be sorted are distinct, the expected number of comparisons performed in the average case by randomized bogosort is asymptotically equivalent to (e-1)n! , and the expected number of swaps in the average case equals (n-1)n! . The expected number of swaps grows faster than the expected number of comparisons, because if the elements are not in order, this will usually be discovered after only a few comparisons, no matter how many elements there are; but the work of shuffling the collection is proportional to its size. In the worst case, the number of comparisons and swaps are both unbounded, for the same reason that a tossed coin might turn up heads any number of times in a row. The best case occurs if the list as given is already sorted; in this case the expected number of comparisons is n - 1 , and no swaps at all are carried out. For any collection of fixed size, the expected running time of the algorithm is finite for much the same reason that the infinite monkey theorem holds: there is some probability of getting the right permutation, so given an unbounded number of tries it will almost surely eventually be chosen. ==Related algorithms== ==See also== Las Vegas algorithm Stooge sort ==References== ==External links== BogoSort on WikiWikiWeb Inefficient sort algorithms Bogosort: an implementation that runs on Unix-like systems, similar to the standard sort program. and jmmcg::bogosort: Simple, yet perverse, C++ implementations of the bogosort algorithm. Bogosort NPM package: bogosort implementation for Node.js ecosystem. Max Sherman Bogo-sort is Sort of Slow, June 2013

source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"Bogosort". 1788004530. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1371942247

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·