728x90
반응형
https://www.acmicpc.net/problem/2751
Python
import sys
input = sys.stdin.readline
n = int(input())
arr = [int(input()) for _ in range(n)]
def merge(low, mid, high):
i, j = low, mid+1
sort = []
while i <= mid and j <= high:
if arr[i] == arr[j]:
sort.append(arr[i])
sort.append(arr[j])
i, j = i+1, j+1
elif arr[i] < arr[j]:
sort.append(arr[i])
i += 1
elif arr[i] > arr[j]:
sort.append(arr[j])
j += 1
while i <= mid:
sort.append(arr[i])
i += 1
while j <= high:
sort.append(arr[j])
j += 1
arr[low:high+1] = sort
def mergeSort(low, high):
if low < high:
mid = (low+high) // 2
mergeSort(low, mid)
mergeSort(mid+1, high)
return merge(low, mid, high)
mergeSort(0, n-1)
for a in arr:
print(a)
728x90
반응형
'Algorithm Problems' 카테고리의 다른 글
[백준][Python] 5525번 IOIOI - 문자열 (0) | 2021.08.04 |
---|---|
[백준][Python] 15828번 Router - 원형 큐 (0) | 2021.08.01 |
[백준][Python] 18258번 큐2, 10866번 덱 - 연결 리스트 (0) | 2021.07.29 |
[백준/Python] 14717번 앉았다 - BruteForce (0) | 2021.07.20 |
[백준/Python] 숫자 정사각형 - BruteForce (0) | 2021.07.20 |