2751번_수 정렬하기 2

sz L·2023년 2월 20일
0

백준 알고리즘

목록 보기
18/32
post-thumbnail

문제
N개의 수가 주어졌을 때, 이를 오름차순으로 정렬하는 프로그램을 작성하시오.

입력
첫째 줄에 수의 개수 N(1 ≤ N ≤ 1,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 수가 주어진다. 이 수는 절댓값이 1,000,000보다 작거나 같은 정수이다. 수는 중복되지 않는다.

출력
첫째 줄부터 N개의 줄에 오름차순으로 정렬한 결과를 한 줄에 하나씩 출력한다.

예제 입력 1
5
5
4
3
2
1
예제 출력 1
1
2
3
4
5

import sys
input = sys.stdin.readline

def merge_sort(s, e):
    if e-s < 1: return
    m = int(s + (e - s) / 2)
    merge_sort(s, m)
    merge_sort(m + 1 , e)
    for i in range(s, e + 1):
        tmp[i] = A[i]
    k = s        
    index1 = s
    index2 = m + 1
    while index1 <= m and index2 <= e:
        if tmp[index1] > tmp[index2]:
            A[k] = tmp[index2]
            k += 1
            index2 += 1
        else:
            A[k] = tmp[index1]
            k += 1
            index1 += 1
    while index1 <= m:
        A[k] = tmp[index1] 
        k += 1
        index1 += 1
    while index2 <= e:
        A[k] = tmp[index2]
        k += 1
        index2 += 1 
       
N = int(input())
A = [0] * int(N + 1)
tmp = [0] * int(N + 1)

for i in range(1, N + 1):
    A[i] = int(input())

merge_sort(1,N)

for i in range(1, N + 1):
    print(str(A[i]))    
 import sys
input = sys.stdin.readline
print = sys.stdout.write

N = int(input())
A = [0] * int(N + 1)

for i in range(1, N+1):
    A[i] = int(input())

A.sort()

for i in range(1, N+1):
    print(str(A[i]) + '\n')

결과

profile
가랑비는 맞는다 하지만 폭풍은 내 것이야

0개의 댓글