Skip to content

added timsort.py #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 15, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions sorts/timsort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
def binary_search(lst, item, start, end):
if start == end:
if lst[start] > item:
return start
else:
return start + 1
if start > end:
return start

mid = (start + end) // 2
if lst[mid] < item:
return binary_search(lst, item, mid + 1, end)
elif lst[mid] > item:
return binary_search(lst, item, start, mid - 1)
else:
return mid


def insertion_sort(lst):
length = len(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:]

return lst


def merge(left, right):
if not left:
return right

if not right:
return left

if left[0] < right[0]:
return [left[0]] + merge(left[1:], right)

return [right[0]] + merge(left, right[1:])


def timsort(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])
runs.append(new_run)
break

if lst[i] < lst[i - 1]:
if not new_run:
runs.append([lst[i - 1]])
new_run.append(lst[i])
else:
runs.append(new_run)
new_run = []
else:
new_run.append(lst[i])

for run in runs:
sorted_runs.append(insertion_sort(run))

for run in sorted_runs:
sorted_array = merge(sorted_array, run)

return sorted_array


def main():

lst = [5,9,10,3,-4,5,178,92,46,-18,0,7]
sorted_lst = timsort(lst)
print(sorted_lst)

if __name__ == '__main__':
main()