From 7c2623f2440b1dccd44861399a20f98f07898cd7 Mon Sep 17 00:00:00 2001 From: Arvind Krishna <54891738+ArvindAROO@users.noreply.github.com> Date: Wed, 15 Apr 2020 15:42:51 +0530 Subject: [PATCH 1/5] added sleepsort Adding sleepsort --- sorts/sleepsort.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 sorts/sleepsort.py diff --git a/sorts/sleepsort.py b/sorts/sleepsort.py new file mode 100644 index 000000000000..cbc027796b86 --- /dev/null +++ b/sorts/sleepsort.py @@ -0,0 +1,35 @@ +""" +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) + +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 + +But this function is not stable for very large values +""" +from time import sleep +from threading import Timer + +def sleepsort(values): + sleepsort.result = [] + def add1(x): + sleepsort.result.append(x) + mx = values[0] + for v in values: + if mx < v: + mx = v + Timer(v, add1, [v]).start() + sleep(mx+1) + return sleepsort.result + +if __name__ == '__main__': + x = [3,2,4,7,3,6,9,1] + sorted_x=sleepsort(x) + print(sorted_x) From 2f74eb3b824e5708f5cd08d686c6ac351e589183 Mon Sep 17 00:00:00 2001 From: John Law Date: Thu, 7 May 2020 14:57:06 +0200 Subject: [PATCH 2/5] Add doctest and typing for sleepsort --- sorts/sleepsort.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/sorts/sleepsort.py b/sorts/sleepsort.py index cbc027796b86..52074f2cdc28 100644 --- a/sorts/sleepsort.py +++ b/sorts/sleepsort.py @@ -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) @@ -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) From 85ff58b2da5f695d81c28bc23a6ac6d2cfe0ce89 Mon Sep 17 00:00:00 2001 From: John Law Date: Thu, 7 May 2020 14:59:17 +0200 Subject: [PATCH 3/5] Use self-descriptive variable name --- sorts/sleepsort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorts/sleepsort.py b/sorts/sleepsort.py index 52074f2cdc28..6767437e842a 100644 --- a/sorts/sleepsort.py +++ b/sorts/sleepsort.py @@ -28,13 +28,13 @@ def sleepsort(values: List[int]) -> List[int]: [1, 2, 2, 3, 4, 8, 9] """ sleepsort.result = [] - def add1(x): + def append_to_result(x): sleepsort.result.append(x) mx = values[0] for v in values: if mx < v: mx = v - Timer(v, add1, [v]).start() + Timer(v, append_to_result, [v]).start() sleep(mx+1) return sleepsort.result From 6380da72030c946f5cdf9c8584c823eddb8605c0 Mon Sep 17 00:00:00 2001 From: John Law Date: Thu, 7 May 2020 15:36:34 +0200 Subject: [PATCH 4/5] Update sleepsort.py --- sorts/sleepsort.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sorts/sleepsort.py b/sorts/sleepsort.py index 6767437e842a..9af7ddbdc95d 100644 --- a/sorts/sleepsort.py +++ b/sorts/sleepsort.py @@ -12,6 +12,8 @@ Hence each of the functions wake in sorted form. This function is not stable for very large values. + +https://rosettacode.org/wiki/Sorting_algorithms/Sleep_sort """ from time import sleep @@ -41,6 +43,6 @@ def append_to_result(x): if __name__ == '__main__': import doctest doctest.testmod - x = [3,2,4,7,3,6,9,1] - sorted_x=sleepsort(x) + x = [3, 2, 4, 7, 3, 6, 9, 1] + sorted_x = sleepsort(x) print(sorted_x) From 70c95f2f2ac0344fdc980bd88981f05da1c1525c Mon Sep 17 00:00:00 2001 From: John Law Date: Thu, 7 May 2020 21:16:59 +0200 Subject: [PATCH 5/5] Update sorts/sleepsort.py Co-authored-by: Christian Clauss --- sorts/sleepsort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/sleepsort.py b/sorts/sleepsort.py index 9af7ddbdc95d..5fa688d1bbd6 100644 --- a/sorts/sleepsort.py +++ b/sorts/sleepsort.py @@ -42,7 +42,7 @@ def append_to_result(x): if __name__ == '__main__': import doctest - doctest.testmod + doctest.testmod() x = [3, 2, 4, 7, 3, 6, 9, 1] sorted_x = sleepsort(x) print(sorted_x)