Bogo Sort (Stupid Sort)
Randomizes array until sorted. Worst-case unbounded. Used only for humor/education.
Visualization
Interactive visualization for Bogo Sort (Stupid Sort)
Original array: 3, 1, 2
Attempts: 0
Bogo Sort (Stupid Sort):
- • Randomly shuffles until sorted
- • Worst-case: unbounded
- • Average: O(n × n!)
- • Used only for humor!
Interactive visualization with step-by-step execution
Implementation
1function bogoSort(arr: number[]): number[] {
2 const a = [...arr];
3 const isSorted = () => a.every((v, i) => i === 0 || a[i-1] <= v);
4 const shuffle = () => {
5 for (let i = a.length - 1; i > 0; i--) {
6 const j = Math.floor(Math.random() * (i + 1));
7 [a[i], a[j]] = [a[j], a[i]];
8 }
9 };
10 while (!isSorted()) shuffle();
11 return a;
12}Deep Dive
Theoretical Foundation
Shuffle randomly, check if sorted, repeat. Expected O((n+1)!) comparisons. Demonstrates inefficiency.
Complexity
Time
O(n)
O((n+1)!)
Unbounded
Space
O(1)
Applications
Industry Use
Educational: worst-case demonstration
Humor
Use Cases
Related Algorithms
Quicksort
A highly efficient, in-place sorting algorithm that uses divide-and-conquer strategy. Invented by Tony Hoare in 1959, it remains one of the most widely used sorting algorithms due to its excellent average-case performance and cache efficiency.
Merge Sort
A stable, divide-and-conquer sorting algorithm with guaranteed O(n log n) performance.
Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping adjacent elements if they are in the wrong order. This process is repeated until the entire array is sorted. Named for the way larger elements 'bubble' to the top (end) of the array.
Insertion Sort
Insertion Sort is a simple, intuitive sorting algorithm that builds the final sorted array one element at a time. It works similarly to how people sort playing cards in their hands - picking each card and inserting it into its correct position among the already sorted cards. Despite its O(n²) time complexity, Insertion Sort is efficient for small datasets and nearly sorted arrays, making it practical for real-world applications.