C샵 버킷정렬 구현

1 개요[ | ]

C# 버킷정렬 구현
C샵 버킷정렬 구현
using System;
using System.Collections;
class Program {
    static void bucketSort(double[] a) {
        int i, size=a.Length;
        ArrayList[] buckets = new ArrayList[size];
        for(i=0; i<size; i++) buckets[i] = new ArrayList();
        foreach(double x in a) { buckets[(int)x*size].Add(x); }
        foreach(ArrayList bucket in buckets) bucket.Sort();
        int pos = 0;
        foreach(ArrayList bucket in buckets) {
            for(i=0; i<bucket.Count; i++) a[pos++]=(double)bucket[i];
        }
    }
    static void Main() {
        double[] arr = {0.9, 0.1, 0.22, 0.99, 0.0, 0.4, 0.22};
        bucketSort( arr );
        Console.Write(string.Join(", ",arr));
        // 0, 0.1, 0.22, 0.22, 0.4, 0.9, 0.99
    }
}

2 같이 보기[ | ]

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