forked from DSC-COEA-Ambajogai/Hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge sorting.py
More file actions
40 lines (32 loc) · 768 Bytes
/
merge sorting.py
File metadata and controls
40 lines (32 loc) · 768 Bytes
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
c = 0
def merge(arr):
if len(arr) > 1:
mid = len(arr) // 2
L = arr[:mid]
R = arr[mid:]
merge(L)
merge(R)
i = j = k = 0
while i < len(L) and j < len(R):
if L[i] < R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
global c
c += len(L) - i
k += 1
while i < len(L):
arr[k] = L[i]
i += 1
k += 1
# c += 1
while j < len(R):
arr[k] = R[j]
j += 1
k += 1
# c += 1
arr = list(map(int, input().split()))
merge(arr)
print(c)