public class Node{
Object element;
Node next;
Node(Object obj,Node nextval){
element = obj;
next = nextval;
}
Node(Node nextval){
next = nextval;
}
public Node getNext(){
return next;
}
public void setNext(Node nextval){
next = nextval;
}
public Object getElement(){
return element;
}
public void setElement(Object obj){
element = obj;
}
@Override
public String toString(){
return element.toString();
}
}
public interface Queue{
public void append(Object obj) throws Exception;
public Object delete() throws Exception;
public Object getFront() throws Exception;
public boolean notEmpty();
}
public class LinQueue implements Queue{
Node front;
Node rear;
int count;
public LinQueue(){
initiate();
}
public LinQueue(int sz){
initiate();
}
private void initiate(){
front = rear = null;
count = 0;
}
@Override
public void append(Object obj){
Node newNode = new Node(obj,null);
if(rear != null)
rear.next = newNode;
rear = newNode;
if(front == null)
front = newNode;
count ++;
}
@Override
public Object delete() throws Exception{
if(count == 0)
throw new Exception("队列已空!");
Node temp = front;
front = front.next;
count --;
return temp.getElement();
}
@Override
public Object getFront() throws Exception{
if(count == 0)
throw new Exception("队列已空!");
return front.getElement();
}
@Override
public boolean notEmpty(){
return count != 0;
}
}
import com.silvia.javabase.datastructure.queue.LinQueue;
import java.util.Arrays;
public class RadixSort {
public static void main(String[] args) throws Exception{
int[] arr = {351,420,215,845,316,909,203,718,405,252};
System.out.println("原始数组:" + Arrays.toString(arr));
radixSort(arr, 10, 3);
System.out.println("排序完成数组:" + Arrays.toString(arr));
}
/**
* d 进制基数
* m 数据元素最大位数
* @param arr
*/
public static void radixSort(int[] arr, int d, int m) throws Exception{
int i;
int j;
int k;
int l;
int power = 1;
int n = arr.length;
LinQueue[] myQueue = new LinQueue[d];
for (i = 0; i < d; i++) {
LinQueue temp = new LinQueue();
myQueue[i] = temp;
}
for (i = 0; i < m; i++) {
if(i == 0){
power = 1;
}else{
power = power * d;
}
for (j = 0; j < n; j++) {
k = arr[j] / power - (arr[j] / (power * d)) * d;
myQueue[k].append(new Integer(arr[j]));
}
l = 0;
for (j = 0; j < d; j++) {
while(myQueue[j].notEmpty()){
arr[l] = ((Integer)myQueue[j].delete()).intValue();
l++;
}
}
}
}
}