C언어 계수정렬 구현

(C 계수정렬 구현에서 넘어옴)

1 개요[ | ]

C 카운팅정렬 구현
C언어 카운팅정렬 구현
#include <stdio.h>
#include <string.h>
void counting_sort(int arr[], int size) {
	int i, max=arr[0];
	for(i=0; i<size; i++) if( arr[i]>max ) max=arr[i];
	int output[size], count[max+1];
	memset(count, 0, sizeof(count));
	for(i=0; i<size; i++) count[arr[i]]++;
	for(i=1; i<=max; i++) count[i] += count[i-1];
	for(i=0; i<size; i++) {
		output[count[arr[i]]-1] = arr[i];
		count[arr[i]]--;
	}
	for(i=0; i<size; i++) arr[i] = output[i];
}
int main() {
	int arr[] = {3,4,2,1,7,5,8,9,0,6,100,10};
	int size = sizeof(arr)/sizeof(int);
	counting_sort(arr, size);
	for(int i=0; i<size; i++) printf("%d ",arr[i]);
	// 0 1 2 3 4 5 6 7 8 9 10 100 
}

2 같이 보기[ | ]

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