Discover the Quirky World of Sleep Sort, Monkey Sort, and Bead Sort
While classic sorting algorithms like quicksort and mergesort dominate for efficiency, this article explores three whimsical, low‑performance sorting methods—Sleep Sort, Monkey Sort, and Bead Sort—detailing their concepts, visual demonstrations, and Java implementations to highlight the fun side of algorithm design.
In the world of algorithms, many high‑efficiency sorting methods such as quicksort, mergesort, and bucket sort dramatically improve program performance.
However, there also exist several quirky sorting algorithms that are neither efficient nor particularly readable; their purpose is simply to be fun.
1. Sleep Sort
Sleep Sort works by launching a separate thread for each element and letting the thread sleep for a duration proportional to the element’s value before printing it, thus producing a sorted output.
<code>public static void sleepSort(int[] array) {
for (int num : array) {
new Thread(() -> {
try {
Thread.sleep(num);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(num);
}).start();
}
}
public static void main(String[] args) {
int[] array = {10,30,50,60,100,40,150,200,70};
sleepSort(array);
}
</code>2. Monkey Sort
Monkey Sort (often called Bogosort) repeatedly shuffles the array until it happens to be sorted, illustrating a brute‑force random approach.
3. Bead Sort
Bead Sort simulates the physical process of beads falling on an abacus: each integer is represented by a column of beads, and gravity causes the beads to settle, resulting in a sorted sequence when read from the bottom.
The article shows how to model the abacus with a two‑dimensional array, let the beads fall, and then read out the sorted one‑dimensional array.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.