Jump to content

Draft:Sleepsort

From Wikipedia, the free encyclopedia

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.

Sleepsort
ClassSorting
Data structureArray
Worst-case performancetemplate

Characteristics

[edit]

Advantages

[edit]

Sleepsort is relatively easy to code.

Disadvantages

[edit]

Sleepsort does not sort negative numbers and simple arrays with large numbers will take a long time.

Examples of sleepsort code

[edit]

C#

[edit]
using System;
using System.Collections.Generic;
using System.Threading;
 
class Program
{
    int[] input;
    int[] output;
    static void Routine(int num)
    {
        Thread.Sleep(num);
        for (i = 1, i < output.Length, 1++)
        {
            if (output[i] == null) 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);
    }
}