-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path464.py
51 lines (48 loc) · 1.42 KB
/
464.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
class Solution:
"""
@param A: an integer array
@return: nothing
"""
def sortIntegers2(self, A):
# write your code here
if len(A) <= 0:
return []
return Solution.subsort(1, A)
def subsort(self, arr):
if len(arr) <= 1:
return arr
if len(arr) == 2:
if(arr[0] > arr[1]):
return [arr[1], arr[0]]
else:
return arr
else:
size = len(arr)
return Solution.merge(1, Solution.subsort(1, arr[:size//2]), Solution.subsort(1, arr[size//2:]))
def merge(self, arr1, arr2):
res = []
if len(arr1) == 0:
return arr2
if len(arr2) == 0:
return arr1
i, j = 0, 0
while i < len(arr1) and j < len(arr2):
if arr1[i] < arr2[j]:
res.append(arr1[i])
i += 1
else :
res.append(arr2[j])
j += 1
if i == len(arr1):
while j < len(arr2):
res.append(arr2[j])
j += 1
if j == len(arr2):
while i < len(arr1):
res.append(arr1[i])
i += 1
return res
print(Solution.sortIntegers2(1, [3, 2, 1, 4, 5, 7, 6, 8]))
print(Solution.sortIntegers2(1, []))
print(Solution.sortIntegers2(1, [3, 2, 1, 4, 5, 7, 6]))
print(Solution.sortIntegers2(1, [3,2,1,4,5]))