-
Notifications
You must be signed in to change notification settings - Fork 0
/
Google | Sort an Array
34 lines (29 loc) · 930 Bytes
/
Google | Sort an Array
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
from collections import Counter
class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
def mergeSort(nums):
if len(nums) > 1:
mid = len(nums)//2
L = nums[:mid]
R = nums[mid:]
mergeSort(L)
mergeSort(R)
i = j = k = 0
while i < len(L) and j < len(R):
if L[i] < R[j]:
nums[k] = L[i]
i+=1
else:
nums[k] = R[j]
j+=1
k+=1
while i < len(L):
nums[k] = L[i]
i+=1
k+=1
while j < len(R):
nums[k] = R[j]
j+=1
k+=1
mergeSort(nums)
return nums