Skip to content

Added sleep-sort #1867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 7, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions sorts/sleepsort.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
"""
Sleepsort is probably the wierdest of all sorting functions
"""Sleepsort is probably the wierdest of all sorting functions
with time-complexity of O(max(input)+n) which is
quite different from almost all other sorting techniques.
If the number of inputs is small then the complexity
can be approximated to be O(max(input)) which is a constant

If the number of inputs is large then the complexity is
approximately O(n)
If the number of inputs is large, the complexity is
approximately O(n).

This function uses multithreading a kind of higher order programming
and calls n functions, each with a sleep time equal to its number.
Hence each of the functions wake in Sorted form
Hence each of the functions wake in sorted form.

But this function is not stable for very large values
This function is not stable for very large values.
"""

from time import sleep
from threading import Timer

def sleepsort(values):
from typing import List


def sleepsort(values: List[int]) -> List[int]:
"""
Sort the list using sleepsort.
>>> sleepsort([3, 2, 4, 7, 3, 6, 9, 1])
[1, 2, 3, 3, 4, 6, 7, 9]
>>> sleepsort([3, 2, 1, 9, 8, 4, 2])
[1, 2, 2, 3, 4, 8, 9]
"""
sleepsort.result = []
def add1(x):
sleepsort.result.append(x)
Expand All @@ -30,6 +39,8 @@ def add1(x):
return sleepsort.result

if __name__ == '__main__':
import doctest
doctest.testmod
x = [3,2,4,7,3,6,9,1]
sorted_x=sleepsort(x)
print(sorted_x)