From 7766518f2b78299d5ec7aad28510d9eef7c8dd7c Mon Sep 17 00:00:00 2001 From: Matheus Muriel Date: Mon, 10 Oct 2022 17:46:49 -0300 Subject: [PATCH 1/3] Added sleep_sort --- sorts/sleep_sort.py | 66 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 sorts/sleep_sort.py diff --git a/sorts/sleep_sort.py b/sorts/sleep_sort.py new file mode 100644 index 000000000000..15002bf28f66 --- /dev/null +++ b/sorts/sleep_sort.py @@ -0,0 +1,66 @@ +""" +This is a pure Python implementation of the sleep algorithm, +In general, sleep sort works by starting a separate task for +each item to be sorted, where each task sleeps for an interval +corresponding to the item's sort key, then emits the item. +Items are then collected sequentially in time. + +More info on: https://rosettacode.org/wiki/Sorting_algorithms/Sleep_sort + +For doctests run following command: +python -m doctest -v sleep_sort.py +or +python3 -m doctest -v sleep_sort.py +For manual testing run: +python sleep_sort.py +""" + +from time import sleep +from threading import Timer + +def sleep_sort(collection): + """Pure implementation of sleep sort algorithm in Python + + :param collection: some mutable ordered collection with heterogeneous + comparable items inside + :return: the same collection ordered by ascending + + Examples: + >>> sleep_sort([0, 5, 2, 3, 2]) + [0, 2, 2, 3, 5] + >>> sleep_sort([0, 5, 2, 3, 2]) == sorted([0, 5, 2, 3, 2]) + True + >>> sleep_sort([]) == sorted([]) + True + >>> sleep_sort([2, 15, 5]) == sorted([2, 15, 5]) + True + >>> sleep_sort([23, 0, 6, 4, 34]) == sorted([23, 0, 6, 4, 34]) + True + """ + result = [] + + def append_result(x): + result.append(x) + + if len(collection) > 0: + mx = collection[0] + for v in collection: + if mx < v: + mx = v + Timer(v, append_result, [v]).start() + + sleep(mx+1) + + return result + +if __name__ == "__main__": + import doctest + import time + + doctest.testmod() + + user_input = input("Enter numbers separated by a comma:").strip() + unsorted = [int(item) for item in user_input.split(",")] + start = time.process_time() + print(*sleep_sort(unsorted), sep=",") + print(f"Processing time: {time.process_time() - start}") \ No newline at end of file From 8a2f3b3cb3ddeec34045fe47ad53bc20b1f56fe0 Mon Sep 17 00:00:00 2001 From: Matheus Muriel Date: Mon, 10 Oct 2022 17:54:08 -0300 Subject: [PATCH 2/3] Fixes: #{6950} --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 9ef72c403f32..953469ffb9ed 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -993,6 +993,7 @@ * [Selection Sort](sorts/selection_sort.py) * [Shell Sort](sorts/shell_sort.py) * [Shrink Shell Sort](sorts/shrink_shell_sort.py) + * [Sleep Sort](sorts/sleep_sort.py) * [Slowsort](sorts/slowsort.py) * [Stooge Sort](sorts/stooge_sort.py) * [Strand Sort](sorts/strand_sort.py) From dda9b75955fa0de7f163ad565cb1822e4c838eb5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 10 Oct 2022 20:57:17 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/sleep_sort.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/sorts/sleep_sort.py b/sorts/sleep_sort.py index 15002bf28f66..74166dcb1320 100644 --- a/sorts/sleep_sort.py +++ b/sorts/sleep_sort.py @@ -1,9 +1,9 @@ """ This is a pure Python implementation of the sleep algorithm, -In general, sleep sort works by starting a separate task for -each item to be sorted, where each task sleeps for an interval -corresponding to the item's sort key, then emits the item. -Items are then collected sequentially in time. +In general, sleep sort works by starting a separate task for +each item to be sorted, where each task sleeps for an interval +corresponding to the item's sort key, then emits the item. +Items are then collected sequentially in time. More info on: https://rosettacode.org/wiki/Sorting_algorithms/Sleep_sort @@ -15,8 +15,9 @@ python sleep_sort.py """ -from time import sleep from threading import Timer +from time import sleep + def sleep_sort(collection): """Pure implementation of sleep sort algorithm in Python @@ -38,21 +39,22 @@ def sleep_sort(collection): True """ result = [] - + def append_result(x): result.append(x) if len(collection) > 0: mx = collection[0] for v in collection: - if mx < v: + if mx < v: mx = v Timer(v, append_result, [v]).start() - sleep(mx+1) + sleep(mx + 1) return result + if __name__ == "__main__": import doctest import time @@ -63,4 +65,4 @@ def append_result(x): unsorted = [int(item) for item in user_input.split(",")] start = time.process_time() print(*sleep_sort(unsorted), sep=",") - print(f"Processing time: {time.process_time() - start}") \ No newline at end of file + print(f"Processing time: {time.process_time() - start}")