In the world of algorithms, there are many efficient sorting algorithms, such as quicksort, merge sort, bucket sort…… They greatly improve the performance of the program.
However, there are some more exotic sorting algorithms that are neither efficient nor readable. So what’s the point of them? Because it’s fun.
Below, let’s introduce three whimsical sorting algorithms.
1. Sleep sequencing
————— the next day —————
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); }Copy the code
-
}
— — — — — —
(Gray shows his code to Rhubarb)
2. Monkey sort
Maybe it’s abstract, but let’s demonstrate:
3. The bead sorting
Anyone who has seen an abacus knows that there are many round beads strung on thin rods, as follows:
What happens if you put the abacus up? The beads on the abacus slide down to the base of the abacus under the force of gravity, as follows:
What does that mean? Let’s be more specific:
How many beads are in each row before the beads fall? As shown in the figure above, the number of beads in each row is 3, 2, 4, 5, 1, and the sequence is out of order.
When the beads fall, how many beads are there in each row? As shown above, the number of beads in each row becomes 1, 2, 3, 4, 5, and the sequence is orderly!
So can we sort a set of positive integers in a way that mimics falling beads? The answer is yes.
We can simulate an abacus with a two-dimensional array, with the position of the beads set to 1 and the position of the beads set to 0. Then, an unordered array of integers can be converted to the following two-dimensional array:
Next, we simulate the process of the abacus bead dropping so that all elements 1 fall to the bottom of the two-dimensional array:
Finally, the backward “abacus” is converted into a one-dimensional ordered array:
— — the END — — –
Like this article friends, welcome to pay attention to the public number programmer xiao Grey, watch more exciting content
Welcome to long click on the QR code to follow Xiao Grey learning English, you learn more than English!