Skip to content

Instantly share code, notes, and snippets.

@bowbowbow
Last active June 3, 2016 19:29
Show Gist options
  • Save bowbowbow/7fcf49f8f765d74a230e34701d557de7 to your computer and use it in GitHub Desktop.
Save bowbowbow/7fcf49f8f765d74a230e34701d557de7 to your computer and use it in GitHub Desktop.
#-*- coding: utf-8 -*-
#한글 주석을 위해 인코딩 형식 utf-8을 선언함
#counting sort python3 version
#python3 문법으로 작성되었습니다.
#입력 예시 : 1 3 17 5 7
#출력 예시 : 1 3 5 7 17
#입력될 수 있는 숫자의 최대 크기를 의미합니다.
MAX_NUM = 1000
#A는 입력된 숫자를 저장하는 배열
A = list(map(int, input().split()))
#N은 입력된 숫자의 개수 입니다.
N = len(A)
#0으로 초기화된 입력될 MAX_NUM+1 길이의 배열 count를 생성합니다.
count =[0]*(MAX_NUM+1)
#countSum은 누적합을 저장하는 배열입니다.
countSum =[0]*(MAX_NUM+1)
#숫자 등장 횟수 세기
for i in range(0, N):
count[A[i]] += 1
#숫자 등장 횟수 누적합 구하기
countSum[0] = count[0]
for i in range(1, MAX_NUM+1):
countSum[i] = countSum[i-1]+count[i];
#B는 정렬된 결과를 저장하는 배열
B = [0]*(N+1)
for i in range(N-1, -1, -1):
B[countSum[A[i]]] = A[i]
countSum[A[i]] -= 1
#수열 A를 정렬한 결과인 수열 B를 출력한다.
result = ""
for i in range(1, N+1):
result += str(B[i]) + " "
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment