Algorithm Problems

[정올] [Python] 3519번 병합정렬

WakaraNai 2021. 4. 3. 21:00
728x90
반응형

www.jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=2859&sca=30

 

JUNGOL

 

www.jungol.co.kr

 

 

 

def mergesort(a, low, high, b):
    if low >= high:
        return

    mid = (low+high)//2

    mergesort(a, low, mid, b)
    mergesort(a, mid+1, high, b)

    i = low
    j = mid+1

    for k in range(low,high+1):
        if j> high:
            b[k] = a[i]
            i+=1
        elif i>mid:
            b[k] = a[j]
            j+=1
        elif a[i]<=a[j]:
            b[k] = a[i]
            i+=1
        else:
            b[k] = a[j]
            j+=1

    for i in range(low,high+1):
        a[i]=b[i]
    for num in a:
        print(num,end=" ")
    print()
        
n=int(input())
nums = list(map(int, input().split()))


mergesort(nums, 0, n-1, [0]*n)
    
728x90
반응형