MCPcopy Create free account
hub / github.com/BeeBombshell/Python-DSA / merge_sort

Function merge_sort

Sorting Algorithms/mergesort.py:1–32  ·  view source on GitHub ↗

list->list sort the inputed list in O(o*log n) time with O(n) space complexity

(listt)

Source from the content-addressed store, hash-verified

1def merge_sort(listt):
2 '''
3 list->list
4 sort the inputed list in O(o*log n) time with O(n) space complexity
5 '''
6 mid=len(listt)//2
7 if(len(listt)>1): #if less than or equal to one then it is already sorted so return the same list in that case
8 left=listt[:mid]
9 right=listt[mid:]
10 merge_sort(left)
11 merge_sort(right)
12
13 i=j=k=0
14
15 while i<len(left) and j<len(right):
16 if left[i]>right[j]:
17 listt[k]=right[j]
18 k+=1
19 j+=1
20 else:
21 listt[k]=left[i]
22 i+=1
23 k+=1
24 while(i<len(left)):
25 listt[k]=left[i]
26 i+=1
27 k+=1
28 while(j<len(right)):
29 listt[k]=right[j]
30 j+=1
31 k+=1
32 return listt
33
34if __name__=='__main__':
35 testlist=[0,3,6,4,2,10,7]

Callers 1

mergesort.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected