Skip to content

Adding Timsort #34

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

Closed
wants to merge 1 commit into from
Closed
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
69 changes: 69 additions & 0 deletions timsort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
def insertion_sort(a,start,end):
n=len(a)

for i in range(start+1,end+1):
key=a[i]
j=i-1
while j>=start and a[j]>key:
a[j+1]=a[j]
j-=1
a[j+1]=key
return a


def merge(a,start,mid,end):
if mid==end:
return a
l = a[start:mid+1]
r = a[mid+1:end+1]
i=0
j=0
arr=[]
while i<len(l) and j<len(r):
if l[i]<r[j]:
a.append(l[i])
i+=1
else:
a.append(r[j])
j+=1

while i<len(l):
a.append(l[i])
i+=1
while j<len(r):
a.append(r[j])
j+=1
return a


def timsort(a):
n=len(a)
min_run=32 #Choosing minimum size to be sorted at one point
for i in range(0,n,min_run):
insertion_sort(a,i,min(n-1,(i+min_run-1)))

size=min_run
while(size<n):
for start in range(0,n,size*2):
mid=(start+size-1)
end=min(n-1,(start+size*2-1))
a=merge(a,start,mid,end) #Two arrays of size = min_run will be merged
size *=2
print("Elements After Sorting:")
print(a)

def print_elements(a):
for ele in a:
print(ele)


if __name__=='__main__':
n = int(input('Enter size of array: '))
a=[]
for i in range(0,n):
ele = int(input())
a.append(ele)

print("Elements Before Sorting:")
print_elements(a)
timsort(a)