C++ 계수정렬 구현

1 개요[ | ]

C++ 계수정렬 구현
#include <iostream>
void counting_sort(int a[], int size) {
	int i, max=a[0];
	for(i=0; i<size; i++) if( a[i]>max ) max=a[i];
	int output[size], count[max+1] = {0};
	for(i=0; i<size; i++) count[a[i]]++;
	for(i=1; i<=max; i++) count[i] += count[i-1];
	for(i=0; i<size; i++) {
		output[count[a[i]]-1] = a[i];
		count[a[i]]--;
	}
	for(i=0; i<size; i++) a[i] = output[i];
}
int main() {
	int arr[] = {9,1,22,4,0,1,22,100,10};
	counting_sort(arr, sizeof(arr)/sizeof(int));
	for(int x: arr) std::cout << x << " ";
	// 0 1 1 4 9 10 22 22 100 
}

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}