Sleepsort
From EverybodyWiki Bios & Wiki
Sleepsort is a Sorting algorithm that takes an array of numbers, gives each number in said array a thread and puts it to sleep for the duration of the number. Due to major drawbacks, it is not considered a useful algorithm in most circumstances.
| Class | Sorting |
|---|---|
| Data structure | Array |
| Worst-case performance | template |
Characteristics
Advantages
Sleepsort is relatively easy to code.
Disadvantages
Sleepsort does not sort negative numbers and simple arrays with large numbers will take a long time.
Examples of sleepsort code
C#
using System;
using System.Collections.Generic;
using System.Threading;
class Program
{
int[] input;
int[] output;
static void Routine(int num)
{
Thread.Sleep(num);
for (int i = 0; i < output.Length; i++)
{
if (output[i] == 0) output[i] = num;
}
}
static void SleepSort(int[] arr)
{
List<Thread> threads = new List<Thread>();
foreach (int num in arr)
{
Thread thread = new Thread(() => Routine(num));
threads.Add(thread);
thread.Start();
}
foreach (Thread thread in threads)
{
thread.Join();
}
}
static void Main()
{
output = new int[input.Length];
SleepSort(input);
}
}
This article "Sleepsort" is from Wikipedia. The list of its authors can be seen in its historical and/or the page Edithistory:Sleepsort. Articles copied from Draft Namespace on Wikipedia could be seen on the Draft Namespace of Wikipedia and not main one.
