From ee914c751ce2b9dc2ca573a3375e07c074e033cd Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 25 Aug 2020 15:48:04 +0200 Subject: [PATCH] Delete sleep_sort.py (#2352) * Delete sleep_sort.py A silly algorithm designed to waste time. #2350 demonstrates that it is a 20+ second denial of service attack on every Travis CI run that we do. * fixup! Format Python code with psf/black push Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- sorts/sleep_sort.py | 49 --------------------------------------------- 1 file changed, 49 deletions(-) delete mode 100644 sorts/sleep_sort.py diff --git a/sorts/sleep_sort.py b/sorts/sleep_sort.py deleted file mode 100644 index 0feda9c5e..000000000 --- a/sorts/sleep_sort.py +++ /dev/null @@ -1,49 +0,0 @@ -""" -Sleep sort 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, 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 function wakes -in sorted time. - -This function is not stable for very large values. - -https://rosettacode.org/wiki/Sorting_algorithms/Sleep_sort -""" -from threading import Timer -from time import sleep -from typing import List - - -def sleep_sort(values: List[int]) -> List[int]: - """ - Sort the list using sleepsort. - >>> sleep_sort([3, 2, 4, 7, 3, 6, 9, 1]) - [1, 2, 3, 3, 4, 6, 7, 9] - >>> sleep_sort([3, 2, 1, 9, 8, 4, 2]) - [1, 2, 2, 3, 4, 8, 9] - """ - sleep_sort.result = [] - - def append_to_result(x): - sleep_sort.result.append(x) - - mx = values[0] - for value in values: - if mx < value: - mx = value - Timer(value, append_to_result, [value]).start() - sleep(mx + 1) - return sleep_sort.result - - -if __name__ == "__main__": - import doctest - - doctest.testmod() - - print(sleep_sort([3, 2, 4, 7, 3, 6, 9, 1]))