数据结构 第三节 基数排序

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++;
                }
            }
        }
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值