C샵 기수정렬 구현

1 개요[ | ]

C샵 기수정렬 구현
using System;
using System.Linq;
class Program {
    static void radixSort(int[] a) {
        radixSort(a, 10);
    }
    static void radixSort(int[] a, int bs) {
        int max = a.Max();
        int exp, i, j, size=a.Length;
        for(exp=1; exp<=max; exp*=bs) {
            int[] count = new int[bs];
            int[] output = new int[size];
            for(i=0; i<size; i++) count[(a[i]/exp)%bs]++;
            for(i=1; i<bs; i++) count[i]+=count[i-1];
            for(i=size-1; i>-1; i--) {
                j = (a[i]/exp)%bs;
                output[count[j]-1] = a[i];
                count[j]--;
            }
            for(i=0; i<size; i++) a[i]=output[i];
        }
    }
    static void Main() {
        int[] arr = {9,1,22,4,0,1,22,100,10};
        radixSort(arr);
        Console.Write(string.Join(",",arr));
        // 0,1,1,4,9,10,22,22,100
    }
}

2 같이 보기[ | ]

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