From 9c4907e7538f6c3abaf95a9c7b7ef0d76ec7406e Mon Sep 17 00:00:00 2001 From: Mihhail Matvejev Date: Wed, 30 Oct 2024 13:13:52 +0300 Subject: [PATCH] fixing logic --- .flake8 | 2 +- timsort/solution.py | 24 ++++++++++++++++++++---- timsort/tests.py | 22 +++++----------------- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/.flake8 b/.flake8 index 659c8d2..978cdbf 100644 --- a/.flake8 +++ b/.flake8 @@ -1,7 +1,7 @@ [flake8] inline-quotes = double ignore = E501, W503, F401 -max-line-length = 100 +max-line-length = 79 exclude = .git, __pycache__, diff --git a/timsort/solution.py b/timsort/solution.py index e3b4d3c..769364f 100644 --- a/timsort/solution.py +++ b/timsort/solution.py @@ -1,5 +1,6 @@ from algo.Stack import Stack + def calculate_minrun(n): count = 0 while n >= 64: @@ -7,6 +8,7 @@ def calculate_minrun(n): n >>= 1 return n + count + def insertion_sort(arr, left, right): for i in range(left + 1, right + 1): key_item = arr[i] @@ -16,10 +18,22 @@ def insertion_sort(arr, left, right): j -= 1 arr[j + 1] = key_item + +def binary_search(arr, key, start): + left = start + right = len(arr) + while left < right: + mid = (left + right) // 2 + if arr[mid] < key: + left = mid + 1 + else: + right = mid + return left + + def merge(arr, start, mid, end): left = arr[start:mid] right = arr[mid:end] - i = j = k = 0 gallop_trigger = 7 count = 0 @@ -37,12 +51,12 @@ def merge(arr, start, mid, end): if count >= gallop_trigger: if left[i] <= right[j]: idx = binary_search(right, left[i], j) - arr[start + k:start + k + idx - j] = right[j:idx] + arr[start + k : start + k + idx - j] = right[j:idx] k += idx - j j = idx else: idx = binary_search(left, right[j], i) - arr[start + k:start + k + idx - i] = left[i:idx] + arr[start + k : start + k + idx - i] = left[i:idx] k += idx - i i = idx while i < len(left): @@ -55,6 +69,7 @@ def merge(arr, start, mid, end): j += 1 k += 1 + def find_runs(arr, minrun): n = len(arr) runs = [] @@ -82,6 +97,7 @@ def find_runs(arr, minrun): return runs + def timsort(arr): n = len(arr) minrun = calculate_minrun(n) @@ -99,4 +115,4 @@ def timsort(arr): else: stack.push(run_a) stack.push(run_b) - break \ No newline at end of file + break diff --git a/timsort/tests.py b/timsort/tests.py index 9d36b89..fb05073 100644 --- a/timsort/tests.py +++ b/timsort/tests.py @@ -1,6 +1,9 @@ import random import unittest +from solution import timsort + + class TestTimSort(unittest.TestCase): def test_empty_array(self): arr = [] @@ -32,27 +35,12 @@ def test_duplicates(self): timsort(arr) self.assertEqual(arr, [1, 1, 2, 2, 3, 3]) - def test_large_sorted_array(self): - arr = list(range(10000)) - expected = arr[:] - random.shuffle(arr) - timsort(arr) - self.assertEqual(arr, expected) - def test_large_reverse_array(self): + def test_large_array(self): arr = list(range(10000, 0, -1)) expected = sorted(arr) timsort(arr) self.assertEqual(arr, expected) - def test_large_random_array(self): - arr = [random.randint(0, 10000) for _ in range(10000)] - expected = sorted(arr) - timsort(arr) - self.assertEqual(arr, expected) - def test_large_array_with_duplicates(self): - arr = [random.choice([1, 2, 3, 4, 5]) for _ in range(10000)] - expected = sorted(arr) - timsort(arr) - self.assertEqual(arr, expected) \ No newline at end of file +