-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodulo sort.py
More file actions
30 lines (27 loc) · 924 Bytes
/
Copy pathmodulo sort.py
File metadata and controls
30 lines (27 loc) · 924 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
# https://www.hackerearth.com/practice/algorithms/sorting/merge-sort/practice-problems/algorithm/monk-and-modulo-based-sorting/
def merge(output, A, B, mod):
i = j = m = 0
while i < len(A) and j < len(B):
if A[i]%mod <= B[j]%mod:
output[m] = A[i]; i+=1; m+=1
else:
output[m] = B[j]; j+=1; m+=1
while i < len(A):
output[m] = A[i]; i+=1; m+=1
while j < len(B):
output[m] = B[j]; j+=1; m+=1
return output
def mergesort(arr, mod):
if len(arr) > 1:
mid = len(arr)//2
left = arr[:mid]
right = arr[mid:]
mergesort(left, mod)
mergesort(right, mod)
return merge(arr, left, right, mod) #only last call will have meaningful return
inp = input().split(" ")
mod = int(inp[1])
arr = [int(x) for x in input().split(" ")]
outp = mergesort(arr, mod)
outp = str(outp).replace(",", "")
print(outp[1:len(outp)-1])