Skip to content

Commit

Permalink
timsort cov
Browse files Browse the repository at this point in the history
  • Loading branch information
mjirik committed Jan 30, 2020
1 parent a1ee5f2 commit bfc589a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
29 changes: 23 additions & 6 deletions micrant/timsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class TimCoupleGenerator():
def __init__(self):
self.sorted_array = None
self.left_is_lower = True
self._insertion_sort_retval = None
self._binary_search_retval = None
self._merge_retval = None

def binary_search(self, the_array, item, start, end):
if start == end:
Expand All @@ -26,13 +29,15 @@ def binary_search(self, the_array, item, start, end):
yield the_array[mid] , item
# if the_array[mid] < item:
if self.left_is_lower:
return self.binary_search(the_array, item, mid + 1, end)
yield from self.binary_search(the_array, item, mid + 1, end)
return self._binary_search_retval

else :
yield item, the_array[mid]
if self.left_is_lower:
# if the_array[mid] > item:
return self.binary_search(the_array, item, start, mid - 1)
yield from self.binary_search(the_array, item, start, mid - 1)
return self._binary_search_retval

else:
return mid
Expand All @@ -48,8 +53,12 @@ def insertion_sort(self, the_array):
l = len(the_array)
for index in range(1, l):
value = the_array[index]
pos = self.binary_search(the_array, value, 0, index - 1)
yield from self.binary_search(the_array, value, 0, index - 1)
pos = self._binary_search_retval
the_array = the_array[:pos] + [value] + the_array[pos:index] + the_array[index + 1:]

self._insertion_sort_retval = the_array

return the_array

# def insertion_sort(the_array):
Expand All @@ -73,11 +82,14 @@ def merge(self, left, right):
if not right:
return left
# TODO yield
print(f"types {type(left)}, {type(right)}")
yield left[0], right[0]
if self.left_is_lower:
yield from self.merge(left[1:], right)
# if left[0] < right[0]:
return [left[0]] + self.merge(left[1:], right)
return [right[0]] + self.merge(left, right[1:])
return [left[0]] + self._merge_retval
yield from self.merge(left, right[1:])
return [right[0]] + self._merge_retval


def timsort(self, the_array):
Expand Down Expand Up @@ -111,11 +123,16 @@ def timsort(self, the_array):

# for every item in runs, append it using insertion sort
for item in runs:
sorted_runs.append(self.insertion_sort(item))
yield from self.insertion_sort(item)
sorted_runs.append(self._insertion_sort_retval)

print("=====")
# for every run in sorted_runs, merge them
sorted_array = []
print(f"sorted array: {sorted_array}")
for run in sorted_runs:

print(f"run: {run}")
sorted_array = self.merge(sorted_array, run)

# print(sorted_array)
Expand Down
27 changes: 27 additions & 0 deletions tests/timsort_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#! /usr/bin/python
# -*- coding: utf-8 -*-

# import logging
# logger = logging.getLogger(__name__)
from loguru import logger
from micrant import timsort

def test_timsort():
cg = timsort.TimCoupleGenerator()
#
# for couple in cg.timsort([5,6,8, 1, 3]):
# i1, i2 = couple
# print(f"i1: {i1}, i2: {i2}")
# if i1 < i2:
# cg.left_is_lower = True
# else:
# cg.left_is_lower = False
#
#
# print(list(cg.sorted_array))
#
# for items in zip(cg.sorted_array, [1,3,5,6,7]):
# i1, i2 = items
# assert i1 == i2


0 comments on commit bfc589a

Please sign in to comment.