Unlock Hidden Code Gems: Shuffle, Sleep Sort, AI One‑Liner & Fast Inverse Square Root
This article showcases a collection of intriguing code snippets—including a two‑line shuffle algorithm, the unconventional Sleep Sort technique, a minimalist AI core loop, a simple method to get tomorrow’s timestamp, and the mysterious 0x5f375a86 fast inverse square root trick.
Project address: https://github.com/kelseyhightower/nocode – a GitHub project with 34k stars that claims to provide a lightweight, cross‑platform, fully automated tool that requires no code to deploy, compile, package, install, and run.
Shuffle Algorithm
This two‑line algorithm generates a uniformly random permutation of n elements, equivalent to Java's Collections.shuffle() implementation.
for(int i = n - 1; i >= 0; i--) {
// rand(0, i) generates a random integer in [0, i]
swap(arr[i], arr[rand(0, i)]);
}Sleep Sort
The sleep‑sort technique creates n threads, each associated with one of the n numbers. Each thread sleeps for a duration proportional to its number, then prints the number when it wakes, resulting in a sorted output.
public class SleepSort {
public static void main(String[] args) {
int[] ints = {1,4,7,3,8,9,2,6,5};
SortThread[] sortThreads = new SortThread[ints.length];
for(int i = 0; i < sortThreads.length; i++) {
sortThreads[i] = new SortThread(ints[i]);
}
for(int i = 0; i < sortThreads.length; i++) {
sortThreads[i].start();
}
}
}
class SortThread extends Thread {
int ms = 0;
public SortThread(int ms) { this.ms = ms; }
public void run() {
try {
sleep(ms * 10 + 10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(ms);
}
}AI Core Code
A minimalist AI‑style loop that reads input, removes Chinese question particles, and prints the transformed string.
while True:
print(input('').replace('吗','').replace('?','!'))Get Next Day Time
Simple code to pause execution for one day (86,400 seconds).
// Sleep for one day
thread.sleep(86400*1000L);0x5f375a86 – Fast Inverse Square Root
This mysterious constant originates from a famous fast inverse square‑root algorithm used in graphics engines. The original magic number is 0x5f3759df; variations like 0x5f37642f and 0x5f375a86 have been discovered through theoretical analysis and brute‑force search.
Hello World
A classic "Hello World" example, included as a reminder that every programmer starts with this simple program regardless of language.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
