From dad781d927934d69527f260d0ccd9185d7e6f9cf Mon Sep 17 00:00:00 2001 From: FrogBattle <44649323+FrogBattle@users.noreply.github.com> Date: Mon, 8 Jul 2019 13:05:59 +0100 Subject: [PATCH 1/2] Update tim_sort.py Update tim_sort.py The previous algorithm was skipping numbers, according to issue #959, and my own tests. The version I am applying uses a while loop, which works correctly and is easier to compute, as there is no break statement. --- sorts/tim_sort.py | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/sorts/tim_sort.py b/sorts/tim_sort.py index b4032b91aec1..5f8047a72942 100644 --- a/sorts/tim_sort.py +++ b/sorts/tim_sort.py @@ -42,36 +42,26 @@ def merge(left, right): def tim_sort(lst): - runs, sorted_runs = [], [] - length = len(lst) - new_run = [lst[0]] - sorted_array = [] - - for i in range(1, length): - if i == length - 1: - new_run.append(lst[i]) + length=len(lst) + runs,sorted_runs=[], [] + new_run=[lst[0]] + sorted_array=[] + i=1 + while i Date: Tue, 30 Jul 2019 18:06:29 +0200 Subject: [PATCH 2/2] Update tim_sort.py --- sorts/tim_sort.py | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/sorts/tim_sort.py b/sorts/tim_sort.py index 5f8047a72942..b95ff34cf384 100644 --- a/sorts/tim_sort.py +++ b/sorts/tim_sort.py @@ -1,10 +1,6 @@ -from __future__ import print_function def binary_search(lst, item, start, end): if start == end: - if lst[start] > item: - return start - else: - return start + 1 + return start if lst[start] > item else start + 1 if start > end: return start @@ -23,7 +19,7 @@ def insertion_sort(lst): for index in range(1, length): value = lst[index] pos = binary_search(lst, value, 0, index - 1) - lst = lst[:pos] + [value] + lst[pos:index] + lst[index+1:] + lst = lst[:pos] + [value] + lst[pos:index] + lst[index + 1 :] return lst @@ -42,18 +38,30 @@ def merge(left, right): def tim_sort(lst): - length=len(lst) - runs,sorted_runs=[], [] - new_run=[lst[0]] - sorted_array=[] - i=1 - while i>> tim_sort("Python") + ['P', 'h', 'n', 'o', 't', 'y'] + >>> tim_sort((1.1, 1, 0, -1, -1.1)) + [-1.1, -1, 0, 1, 1.1] + >>> tim_sort(list(reversed(list(range(7))))) + [0, 1, 2, 3, 4, 5, 6] + >>> tim_sort([3, 2, 1]) == insertion_sort([3, 2, 1]) + True + >>> tim_sort([3, 2, 1]) == sorted([3, 2, 1]) + True + """ + length = len(lst) + runs, sorted_runs = [], [] + new_run = [lst[0]] + sorted_array = [] + i = 1 + while i < length: + if lst[i] < lst[i - 1]: runs.append(new_run) - new_run=[lst[i]] + new_run = [lst[i]] else: new_run.append(lst[i]) - i+=1 + i += 1 runs.append(new_run) for run in runs: @@ -62,11 +70,14 @@ def tim_sort(lst): sorted_array = merge(sorted_array, run) return sorted_array + + def main(): - lst = [5,9,10,3,-4,5,178,92,46,-18,0,7] + lst = [5, 9, 10, 3, -4, 5, 178, 92, 46, -18, 0, 7] sorted_lst = tim_sort(lst) print(sorted_lst) -if __name__ == '__main__': + +if __name__ == "__main__": main()