Radix Sort Java实现 [英] Radix Sort Java Implementation

查看:164
本文介绍了Radix Sort Java实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对1-100之间的随机数的ArrayList进行基数排序.我快到了,我无法弄清楚十位数的排序.另外,我放置了一些println语句来测试存储桶中的实际内容,并且某些存储桶中不应该存在一些奇怪的数字.

I am attempting to implement a Radix Sort on an ArrayList of random numbers from 1-100. I'm almost there, I just can't figure out the tens place sorting. Also, I put some println statements to test what is actually in my buckets and there are some weird numbers in some buckets that shouldn't be there.

import java.util.ArrayList;
import java.util.Random;

private static ArrayList<Integer> newArrayList;
private static ArrayList<Integer>[] bucket = new ArrayList[10];


public static ArrayList<Integer> generateArray(int n){
newArrayList = new ArrayList<Integer>(n);
Random rand = new Random();
for (int i = 0; i<n; i++){
newArrayList.add(rand.nextInt(n + 1));
}


return newArrayList;
}

public static void sort(ArrayList<Integer> x){
for (int i = 0; i< 10; i++){
    bucket[i] = new ArrayList<Integer>();
}

int power = 1;
for (int j = 0; j < 3; j++){

    for (int k = 0; k < x.size(); k++){
        bucket[(x.get(k)/power)%10].add(x.get(k));
    }
    x.clear();

    for (int l = 0; l<10; l++){
        x.addAll(bucket[l]);
    }

    power = power*10;
    }

System.out.println(bucket[0]); //diagnostics
System.out.println(bucket[1]);
System.out.println(bucket[2]);
System.out.println(bucket[3]);
System.out.println(bucket[4]);
System.out.println(bucket[5]);
System.out.println(bucket[6]);
System.out.println(bucket[7]);
System.out.println(bucket[8]);
System.out.println(bucket[9]);
}
public static void main (String[] args)
  {
    ArrayList<Integer> new1 = new ArrayList<Integer>();
    new1 = generateArray(100);
    sort(new1);
  }



}

这是打印语句的输出.

Here is the output from the print statements.

 [70, 90, 100, 100, 90, 70, 30, 20, 100, 100, 3, 3, 5, 6, 6, 6, 6, 7, 7, 8, 70, 90, 90, 70, 30, 20, 3, 3, 5, 6, 6, 6, 6, 7, 7, 8, 61, 51, 81, 81, 61, 31, 71, 31, 41, 41, 11, 11, 11, 21, 71, 11, 71, 11, 11, 11, 11, 12, 13, 14, 15, 15, 16, 19, 19, 32, 42, 92, 42, 22, 22, 32, 52, 12, 82, 42, 20, 21, 22, 22, 26, 27, 28, 29, 29, 53, 3, 63, 3, 63, 13, 73, 83, 33, 83, 30, 31, 31, 32, 32, 33, 34, 36, 36, 39, 64, 84, 14, 64, 34, 54, 64, 41, 41, 42, 42, 42, 45, 46, 49, 49, 5, 15, 15, 55, 45, 65, 95, 51, 52, 53, 54, 55, 56, 59, 6, 86, 36, 56, 26, 6, 6, 16, 6, 46, 36, 76, 66, 61, 61, 63, 63, 64, 64, 64, 65, 66, 67, 68, 69, 69, 97, 87, 67, 7, 87, 27, 77, 7, 97, 70, 70, 71, 71, 71, 73, 76, 77, 78, 79, 8, 78, 98, 98, 28, 68, 81, 81, 82, 83, 83, 84, 86, 87, 87, 19, 49, 19, 49, 99, 79, 59, 69, 29, 39, 69, 29, 90, 90, 92, 95, 97, 97, 98, 98, 99]

[61, 51, 81, 81, 61, 31, 71, 31, 41, 41, 11, 11, 11, 21, 71, 11, 71, 11, 11, 11, 11, 12, 13, 14, 15, 15, 16, 19, 19, 100, 100, 100, 100]
[32, 42, 92, 42, 22, 22, 32, 52, 12, 82, 42, 20, 21, 22, 22, 26, 27, 28, 29, 29]
[53, 3, 63, 3, 63, 13, 73, 83, 33, 83, 30, 31, 31, 32, 32, 33, 34, 36, 36, 39]
[64, 84, 14, 64, 34, 54, 64, 41, 41, 42, 42, 42, 45, 46, 49, 49]
[5, 15, 15, 55, 45, 65, 95, 51, 52, 53, 54, 55, 56, 59]
[6, 86, 36, 56, 26, 6, 6, 16, 6, 46, 36, 76, 66, 61, 61, 63, 63, 64, 64, 64, 65, 66, 67, 68, 69, 69]
[97, 87, 67, 7, 87, 27, 77, 7, 97, 70, 70, 71, 71, 71, 73, 76, 77, 78, 79]
[8, 78, 98, 98, 28, 68, 81, 81, 82, 83, 83, 84, 86, 87, 87]
[19, 49, 19, 49, 99, 79, 59, 69, 29, 39, 69, 29, 90, 90, 92, 95, 97, 97, 98, 98, 99]

推荐答案

您需要在循环的每次迭代中清除存储桶,否则前一次迭代的值仍然存在

You need to clear the buckets each iteration of the loop otherwise value from the previous iteration will still be there

for (int l = 0; l < 10; l++) {
    x.addAll(bucket[l]);
    bucket[l].clear(); //<-----
}

这篇关于Radix Sort Java实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆