Skip to content

Commit

Permalink
Created Readme for BozoSort
Browse files Browse the repository at this point in the history
  • Loading branch information
ammar0211 authored Oct 7, 2019
1 parent 94b6a97 commit 8017c44
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions sort/BozoSort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Bozo Sort

The idea is very simple: take the list, check if it's sorted, and of it's not, swap two random elements of the list. Proceed like that until the list becomes sorted.

> ## Pseudocode
PseudoCode to bozosort an array -

To bozo sort an array:

While the array is unsorted,

Swap two randomly selected entries.
end

end

> ## Complexity
In the very worst case, Bozosort runs forever -- we could get really unlucky, and keep swapping elements 0 and 1 forever, for example.

We can talk instead about how long Bozosort will take on average. The analysis is not actually easy, but roughly speaking, we can hope that we'll go through every permutation of the list once, on average (we'll come back to some permutations many times, and we will sort the list before getting to some other permutations). There are n! permutations of a list of length n in total, so we can estimate that on average, Bozosort takes O(n!) time.

(Note that actually, is_sorted_nondecreasing() takes O(n) time every time, so saying the runtime is O(n×n!)≈O((n+1)!) is more correct, but it doesn't really make a difference since n! is so large.)

0 comments on commit 8017c44

Please sign in to comment.