From 78916b66964c6241b41b378be2cfff09d57f8767 Mon Sep 17 00:00:00 2001 From: Dawood Wasif Date: Sun, 30 Oct 2022 09:02:57 +0100 Subject: [PATCH 1/2] Added sleep sort --- sorts/sleep_sort.py | 23 +++++++++++++++++++++++ 1 file changed, 23 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..e241528ec823 --- /dev/null +++ b/sorts/sleep_sort.py @@ -0,0 +1,23 @@ +from time import sleep +from threading import Timer + +""" 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): + sleep_sort.result = [] + def add1(x): + sleep_sort.result.append(x) + mx = values[0] + 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] \ No newline at end of file From 5ccb54a66335aad890f81f27345446328b442423 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 30 Oct 2022 15:21:55 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/sleep_sort.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/sorts/sleep_sort.py b/sorts/sleep_sort.py index e241528ec823..f7457f949db2 100644 --- a/sorts/sleep_sort.py +++ b/sorts/sleep_sort.py @@ -1,23 +1,28 @@ -from time import sleep from threading import Timer +from time import sleep -""" In sleep sort, the thread having +""" In sleep sort, the thread having the least amount of sleeping time -wakes up first and the number gets +wakes up first and the number gets printed and hence list is sorted""" + def sleep_sort(values): sleep_sort.result = [] + def add1(x): sleep_sort.result.append(x) + mx = values[0] for v in values: - if mx < v: mx = v + if mx < v: + mx = v Timer(v, add1, [v]).start() - sleep(mx+1) + sleep(mx + 1) return sleep_sort.result + if __name__ == "__main__": - x = [3,2,4,7,3,6,9,1] + x = [3, 2, 4, 7, 3, 6, 9, 1] print(sleep_sort(x)) - #[1, 2, 3, 3, 4, 6, 7, 9] \ No newline at end of file + # [1, 2, 3, 3, 4, 6, 7, 9]