package OOPLS;

import OOPLS.RadixSortElement;
import java.util.LinkedList;
import java.util.Queue;

public class RadixSorter {
    public static void sort(Queue masterQ) {
        RadixSortElement rse = 
            (RadixSortElement)masterQ.element();
        LinkedList[] subQ = new LinkedList[rse.radix()];
        for (int i=0; i<rse.radix(); ++i) 
            subQ[i] = new LinkedList();
        int binNumber, digitPosition = 1;
        RadixSortElement currentItem;
        while (digitPosition <= rse.digitsInKey()) {
            while (!masterQ.isEmpty()) {
                currentItem = 
                    (RadixSortElement)masterQ.element();
                masterQ.remove();
                binNumber = 
                    currentItem.getDigit(digitPosition);
                subQ[binNumber].add(currentItem);
            }
            for (int k=0; k<rse.radix(); ++k) {
                masterQ.addAll(subQ[k]);
                subQ[k].clear();
            }
            digitPosition++;
        }
    }
}
