Sleep Sort Program with Explanation and Code
Sleep Sort is a humorous timing-based sorting algorithm where each number sleeps for its value before printing, causing numbers to appear in sorted order. This page explains the concept and provides implementations in Python, C, JavaScript, PHP, and C#.
Program Explanation
What is Sleep Sort?
Sleep Sort is a humorous and unconventional sorting algorithm that uses time delays to sort numbers. The idea is simple: for each number in the list, create a separate thread or asynchronous process that sleeps for a duration equal to the value of the number. After waking, the thread prints the number. Smaller numbers wake up first, so they appear earlier in the output, creating a sorted sequence.
Although entertaining, Sleep Sort is impractical, inaccurate for large numbers, and completely unusable for non-positive numbers or non-integer values. It is mainly used as a joke or to demonstrate concurrency concepts.
Steps to Solve
- Read the list of non-negative integers.
- Create a separate thread or async function for each element.
- Each thread sleeps for a duration proportional to the element’s value.
- When the sleep ends, the thread outputs the number.
- Numbers automatically appear in sorted (ascending) order.
Key Points
- Works only with non-negative integers.
- Requires multithreading or asynchronous execution.
- Highly inefficient and not a real sorting algorithm.
- Time complexity depends on the largest number's value.
- Used only for fun or teaching threading basics.
Program Code
import threading
import time
def sleeper(num):
time.sleep(num)
print(num, end=" ")
arr = list(map(int, input("Enter non-negative integers: ").split()))
threads = []
for num in arr:
t = threading.Thread(target=sleeper, args=(num,))
threads.append(t)
t.start()
for t in threads:
t.join()
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void* sleeper(void* arg) {
int num = *(int*)arg;
sleep(num);
printf("%d ", num);
return NULL;
}
int main() {
int n;
printf("Enter number of integers: ");
scanf("%d", &n);
int arr[n];
pthread_t threads[n];
printf("Enter non-negative integers: ");
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
for (int i = 0; i < n; i++)
pthread_create(&threads[i], NULL, sleeper, &arr[i]);
for (int i = 0; i < n; i++)
pthread_join(threads[i], NULL);
return 0;
}
function sleepSort(arr) {
arr.forEach(num => {
setTimeout(() => console.log(num), num * 1000);
});
}
let arr = prompt("Enter non-negative integers:").split(" ").map(Number);
sleepSort(arr);
using System;
using System.Threading;
class Program {
static void Sleeper(object numObj) {
int num = (int)numObj;
Thread.Sleep(num * 1000);
Console.Write(num + " ");
}
static void Main() {
Console.Write("Enter non-negative integers: ");
int[] arr = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
Thread[] threads = new Thread[arr.Length];
for (int i = 0; i < arr.Length; i++) {
threads[i] = new Thread(Sleeper);
threads[i].Start(arr[i]);
}
foreach (var t in threads) t.Join();
}
}
<?php
$arr = array_map('intval', explode(" ", readline("Enter non-negative integers: ")));
foreach ($arr as $num) {
usleep($num * 1000000);
echo $num . " ";
}
?>
