forked from kingroryg/DSA
-
Notifications
You must be signed in to change notification settings - Fork 5
/
mergesort.py
64 lines (54 loc) · 1.32 KB
/
mergesort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# ALAINA KAFKES IMPLEMENTATION - alainakafkes
## Mergesort implementation in Python
## Runtime: O(n log(n))
## Space: O(n)
## Advantages: guaranteed good complexity (no need to worry about choice of pivot as in quicksort)
## Disadvantages: more temporary data storage needed than quicksort
#def mergesort(arr):
# if len(arr) <= 1:
# return arr
# midpoint = len(arr) // 2
# return merger(mergesort(arr[:mid]), mergesort(arr[mid:]))
#def merger(arrLeft, arrRight):
# if not arrLeft:
# return arrRight
# if not arrRight:
# return arrLeft
# if arrLeft[0] < arrRight[0]:
# return arrLeft[0] + merger(arrLeft[1:], arrRight)
# return arrRight[0] + merger(arrLeft, arrRight[1:])
# YASH KATARIYA IMPLEMENTATION - yashk2810
def merge(L,R,A): # Recursive and stable(for equal numbers it preserves their original position) algorithm
nL=len(L)
nR=len(R)
i=0
j=0
k=0
while i<nL and j<nR:
if L[i]<R[j]:
A[k]=L[i]
i+=1
else:
A[k]=R[j]
j+=1
k+=1
while i<nL:
A[k]=L[i]
i+=1
k+=1
while j<nR:
A[k]=R[j]
j+=1
k+=1
def mergesort(A): # Time complexity is Worst Case:- O(nlogn)
n=len(A) # Space complexity is O(n)
if n<2:
return A
mid=n/2
left=A[:mid]
right=A[mid:]
mergesort(left)
mergesort(right)
merge(left,right,A)
return A
print mergesort([2,4,1,6,8,5,3])