基数排序——java

基数排序
时间复杂度:O(d*(r+n))
空间复杂度:O(r*d+n)
【r–>关键字的基数,d–>长度,n–>关键字的个数】
稳定
核心代码

import java.util.ArrayList;
import java.util.List;

/**
 * 基数排序
 * @author jin
 *
 */
public class RadixSort {

    public void radix(int[] array) {
        int max = array[0];
        for (int i = 0; i < array.length; i++) { // 得到数组的最大值
            if (max < array[i]) {
                max = array[i];
            }
        }

        int time = 0;
        while (max > 0) { // 判断位数
            max /= 10;
            time++;
        }

        // 建立10个队列
        List<ArrayList> queue = new ArrayList<ArrayList>();
        for (int i = 0; i < 10; i++) {
            ArrayList<Integer> queue1 = new ArrayList<Integer>();
            queue.add(queue1);
        }

        // 进行time次分配和收集
        for (int i = 0; i < time; i++) {
            // 分配数组元素
            for (int j = 0; j < array.length; j++) {
                // 得到数字的第time+1位数
                // Math.pow(double a,double b)为a的b次方
                int x = array[j] % (int) Math.pow(10, i + 1) / (int) Math.pow(10, i);
                ArrayList<Integer> queue2 = queue.get(x);
                queue2.add(array[j]);
                queue.set(x, queue2);

            }

            int count = 0;// 元素计数器;
            // 收集队列元素;
            for (int k = 0; k < 10; k++) {
                while (queue.get(k).size() > 0) {//第k个队列的长度
                    ArrayList<Integer> queue3 = queue.get(k);//把第k个对列给queue3
                    array[count] = queue3.get(0);//把queue3的第一个值值给array
                    queue3.remove(0);//给一次,就删除一次第一个元素
                    count++;
                }
            }

        }

    }


    public void radixSort() {
        int[] a = { 2,35, 684, 557, 44, 644, 801, 99, 70, 0,54,78,23 };
        radix(a);
        for (int i = 0; i < a.length; i++){
            System.out.println(a[i]);
        }
    }  
}

基数排序是从最低位个位开始比较,按0~9的顺序排序;然后再按倒数第二位十位比较……一直到最高位,排完后,就实现了整个数组的排序。
步骤:
(1)先求最大的数有几位数,

    int max=a[0];
    for(int i=1;i<a.length;i++){
        if(max<a[i]){
            max=a[i];
        }
    }
    int radix=0;
    while(max!=0){
        max/=10;
        radix++;
    }

(2)建队列,在这个队列中再建10个队列,代表0~9,这10个数;

Arraylist<ArrayList> queue=new ArrayList<ArrayList>();
for(int i=0;i<10;i++){
    ArrayList<Integer> queue1=new ArrayList<Integer>();
    queue.add(queue1);  
}

相当于:
queue–>[ ];
queue1添加进queue后–>[ [],[],[],[],[],[],[],[],[],[] ];
(3)从最后一位到最高位,依次比较大小,某一位数相同的数,放入一个队列中;

for(int i=0;i<radix;i++){
    for(int j=0;j<a.length;j++){
        int x=a[j]%(int)Math.pow(10,i+1)/(int)Math.pow(10,i);//取得各个位上的数
        ArrayList<Integer> queue2=queue.get(x);//此处的x为queue队列的索引
        queue2.add(a[j]);//把a[j]的元素添加到queue2队列中
        queue.set(x,queue2);//把queue2放入对应的queue队列中
    }
    int count =0;
    for(int k=0;k<10;k++){
        while(queue.get(k).size()>0){
            ArrayList<Integer> queue3=queue.get(k);//从queue队列中取出k对应的元素给queue3
            a[count]=queue3.get(0);//queue3中只有0这个索引,即只有一个元素
            aueue3.remove(0);//把queue3中的元素清空了,以便下一个循环使用
            count++;
        }
    }
}

Math.pow(double a,double b)为a的b次方;
例:

546%(int)Math.pow(10,1)/(int)Math.pow(10,0)=6;
546%(int)Math.pow(10,2)/(int)Math.pow(10,1)=4;
546%(int)Math.pow(10,3)/(int)Math.pow(10,2)=5;

猜你喜欢

转载自blog.csdn.net/levi_moon/article/details/51484389