C샵 계수정렬 구현

Jmnote (토론 | 기여)님의 2018년 7월 16일 (월) 00:17 판 (새 문서: ==개요== ;C# 카운팅정렬 구현 <source lang='csharp'> using System; using System.Linq; class Program { static void count_sort(int []arr) { int i, size = arr.Length, max = a...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요

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

2 같이 보기

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