-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5204_병합정렬.py
50 lines (40 loc) · 1.08 KB
/
5204_병합정렬.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
# 병합정렬
def merge(left, right):
global cnt
res = []
if left[-1] > right[-1]:
cnt += 1
i = j = 0 #i , j left, right 의 인덱스
while i < len(left) and j < len(right):
if left[i] >= right[j]:
if res == []:
res = [right[j]]
else:
res[-1:] = [res[-1], right[j]]
j += 1
else:
if res == []:
res = [left[i]]
else:
res[-1:] = [res[-1], left[i]]
i += 1
if i < len(left):
res.extend(left[i:])
else:
res.extend(right[j:])
return res
def sorting(arr):
mid = len(arr) // 2
if mid == 0: #arr의 길이가 0, 1이면 //2의 값이 2이므로
return arr
l = arr[:mid]
r = arr[mid:]
l = sorting(l)
r = sorting(r)
return merge(l, r)
for test_case in range(int(input())):
N = int(input())
test_arr = list(map(int,input().split()))
cnt = 0
test_arr = sorting(test_arr)
print('#%d %d %d' %(test_case + 1, test_arr[N // 2], cnt))