Java 버킷정렬 구현

1 개요[ | ]

Java 버킷정렬 구현
자바 버킷정렬 구현
import java.util.ArrayList;
import java.util.Collections;
public class MyClass {
    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();
        for(double x: a) { buckets[(int)x*size].add(x); }
        for(ArrayList bucket: buckets) Collections.sort(bucket);
        int pos = 0;
        for(ArrayList bucket: buckets) {
            for(i=0; i<bucket.size(); i++) a[pos++]=(double)bucket.get(i);
        }
    }
    public static void main(String args[]) {
    	double[] arr = {0.9, 0.1, 0.22, 0.99, 0.0, 0.4, 0.22};
    	bucketSort(arr);
    	for(double x: arr) System.out.format( "%s ", java.math.BigDecimal.valueOf(x).stripTrailingZeros() );
    	// 0 0.1 0.22 0.22 0.4 0.9 0.99 
    }
}

2 같이 보기[ | ]

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