[SW Expert Academy, SWEA 2063] 중간값 찾기 (python)
Problem Solving/SWEA

[SW Expert Academy, SWEA 2063] 중간값 찾기 (python)

728x90

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=1&contestProbId=AV5QPsXKA2UDFAUq&categoryId=AV5QPsXKA2UDFAUq&categoryType=CODE&problemTitle=&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=1&pageSize=10&pageIndex=1&&&&&&&&&&

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com


※ SW Expert 아카데미의 문제를 무단 복제하는 것을 금지합니다.

728x90

# bubble sort 수행.
# 중간까지 했을 때 sort 종료하면, sort를 다 진행하지 않아도 중간값을 구할 수 있다.


N = int(input())

lst = list(map(int, input().split()))

for i in range(int(N / 2) + 1):
    for j in range(1, N - i):
        if (lst[j - 1] > lst[j]):
            tmp = lst[j - 1]
            lst[j - 1] = lst[j]
            lst[j] = tmp


print(lst[int(N / 2)])

sort 함수 사용

당연하지만 bubble sort는 시간 복잡도가 $O(n^2)$이기 때문에, sort 함수를 사용하는 것이 더 빠르다.

# bubble sort 수행.
# 중간까지 했을 때 sort 종료하면, sort를 다 진행하지 않아도 중간값을 구할 수 있다.


N = int(input())

lst = list(map(int, input().split()))

lst.sort()

print(lst[int(N / 2)])
728x90