Skip to content

Added sleep sort #7876

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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
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
28 changes: 28 additions & 0 deletions sorts/sleep_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from threading import Timer
from time import sleep

""" In sleep sort, the thread having
the least amount of sleeping time
wakes up first and the number gets
printed and hence list is sorted"""


def sleep_sort(values):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file sorts/sleep_sort.py, please provide doctest for the function sleep_sort

Please provide return type hint for the function: sleep_sort. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: values

sleep_sort.result = []

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file sorts/sleep_sort.py, please provide doctest for the function sleep_sort

Please provide return type hint for the function: sleep_sort. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: values


def add1(x):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file sorts/sleep_sort.py, please provide doctest for the function add1

Please provide return type hint for the function: add1. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: x

Please provide descriptive name for the parameter: x

sleep_sort.result.append(x)

mx = values[0]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file sorts/sleep_sort.py, please provide doctest for the function add1

Please provide return type hint for the function: add1. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: x

Please provide descriptive name for the parameter: x

for v in values:
if mx < v:
mx = v
Timer(v, add1, [v]).start()
sleep(mx + 1)
return sleep_sort.result


if __name__ == "__main__":
x = [3, 2, 4, 7, 3, 6, 9, 1]
print(sleep_sort(x))
# [1, 2, 3, 3, 4, 6, 7, 9]