Takes a list of natural numbers and sorts them using ThreadedBogoMergeSort.
Fun fact: a sorting algorithm called BogoSort shuffles a list randomly until it
becomes sorted; it is
Fun fact 2: TBMSort is multithreaded AND slower than BogoSort.
Here is some pseudocode for bogosort
fn bogosort(rng, arr)
if is_sorted(arr)
arr
else
(rng_, arr_) = shuffle(rng, arr)
bogosort(rng_, arr_)
(You can't just ignore the random number generator.)
Here is some pseudocode for mergesort
fn mergesort(arr)
if length(arr) <= 1
arr
else
n = (length(arr) / 2) as int
l_result = mergesort(arr[..n])
r_result = mergesort(arr[n..])
merge(l_result, r_result)
The merge step in the original mergesort have the precondition that the two
input arrays are already sorted, say, least to greatest; suppose the head
elements of the arrays can be popped off on a one-by-one basis, then the
precondition would guarantee that each head element is
Here is some pseudocode for BogoMergeSort
fn bmsort(rng, arr)
if length(arr) <= 1
arr
else
n = (length(arr) / 2) as int
(rng, l_result) = bmsort(rng, arr[..n])
(rng, r_result) = bmsort(rng, arr[n..])
maybe_result = shuffle(rng, join(l_result, r_result))
if is_sorted(maybe_result)
maybe_result
else
bmsort(rng, maybe_result)
As you can see, it is basically bogosort, except it redoes all lower-level
shuffles. Where upon one "run" the chance for bogosort to complete is
TBMSort is the threaded version of BogoMergeSort, performing the
divide-and-conquer in parallel with threads. For every recursion in the D&C, it
spawns a new thread and uses the thread-local RNG provided by the rand
crate.
The end result is that it makes full use of your CPU while being slow.
usage:
tbmsort NATURALS...
(where NATURALS
is actually usize
.)