"C샵 계수정렬 구현"의 두 판 사이의 차이

6번째 줄: 6번째 줄:
using System.Linq;
using System.Linq;
class Program {
class Program {
static void counting_sort(int []a) {
static void countingSort(int []a) {
int i, size=a.Length, max=a.Max();
int i, size=a.Length, max=a.Max();
int[] output = new int[size];
int[] output = new int[size];
21번째 줄: 21번째 줄:
static void Main() {
static void Main() {
int[] arr = {3,4,2,1,7,5,8,9,0,6,100,10};
int[] arr = {3,4,2,1,7,5,8,9,0,6,100,10};
counting_sort(arr);
countingSort(arr);
foreach(int x in arr) Console.Write("{0} ",x);
        Console.Write(string.Join(",",arr));
// 0 1 2 3 4 5 6 7 8 9 10 100  
// 0,1,2,3,4,5,6,7,8,9,10,100
}
}
}
}

2018년 8월 28일 (화) 20:34 판

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 }}