Skip to content

Commit 5e3780d

Browse files
authored
feat(docs): add 'bogosort' and 'bucketsort' to sorting docs (alexfertel#50)
1 parent 14b53d3 commit 5e3780d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/sorting/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
### [Bogo Sort](./bogo_sort.rs)
2+
3+
From [Wikipedia][bogosort-wiki]: In computer science, bogosort (also known as permutation sort, stupid sort, slowsort or bozosort) 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.
4+
5+
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. 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. Its name is a portmanteau of the words bogus and sort.
6+
7+
[bogosort-wiki]:https://en.wikipedia.org/wiki/Bogosort
8+
9+
### [Bucket_Sort](./bucket_sort.rs)
10+
11+
From [Wikipedia][bucketsort-wiki]: Bucket sort, or bin sort, is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm. It is a distribution sort, a generalization of pigeonhole sort that allows multiple keys per bucket, and is a cousin of radix sort in the most-to-least significant digit flavor. Bucket sort can be implemented with comparisons and therefore can also be considered a comparison sort algorithm. The computational complexity depends on the algorithm used to sort each bucket, the number of buckets to use, and whether the input is uniformly distributed.
12+
13+
Bucket sort works as follows:
14+
15+
Set up an array of initially empty "buckets".
16+
Scatter: Go over the original array, putting each object in its bucket.
17+
Sort each non-empty bucket.
18+
Gather: Visit the buckets in order and put all elements back into the original array.
19+
20+
Optimization:
21+
A common optimization is to put the unsorted elements of the buckets back in the original array first, then run insertion sort over the complete array; because insertion sort's runtime is based on how far each element is from its final position, the number of comparisons remains relatively small, and the memory hierarchy is better exploited by storing the list contiguously in memory.
22+
23+
If the input distribution is known or can be estimated, buckets can often be chosen which contain constant density (rather than merely having constant size). This allows O(n) average time complexity even without uniformly distributed input.
24+
25+
[bucketsort-wiki]: https://en.wikipedia.org/wiki/Bucket_sort
26+
127
### [Bubble Sort](./bubble_sort.rs)
228

329
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order.

0 commit comments

Comments
 (0)