C샵 계수정렬 구현

1 개요[ | ]

C# 카운팅정렬 구현
using System;
using System.Linq;
class Program {
	static void countingSort(int []a) {
		int i, size=a.Length, max=a.Max();
		int[] output = new int[size];
		int[] count = new int[max+1];
		for(i=0; i<max; i++) count[i] = 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];
	}
	static void Main() {
		int[] arr = {3,4,2,1,7,5,8,9,0,6,100,10};
		countingSort(arr);
        Console.Write(string.Join(",",arr));
		// 0,1,2,3,4,5,6,7,8,9,10,100
	}
}

2 같이 보기[ | ]

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