繁体   English   中英

基数排序Java帮助

[英]Radix sort in java help

嗨,我需要一些帮助来改进我的代码。 我正在尝试使用Radixsort对10个数字的数组(例如)进行递增排序。

当我运行数组大小为10的程序并将10个随机整型数放入70 309 450 279 799 192 586 609 54 657

我得到这个:

450 309 192 279 54 192 586 657 54 609

看不到我的错误在代码中的位置。

class IntQueue
{

  static class Hlekkur
  {
    int tala;
    Hlekkur naest;
  }

  Hlekkur fyrsti;
  Hlekkur sidasti;
  int n;

  public IntQueue()
  {
    fyrsti = sidasti = null;
  }


  // First number in queue.
  public int first()
  {
    return fyrsti.tala;
  }


  public int get()
  {
    int res = fyrsti.tala;
    n--;
    if( fyrsti == sidasti )
      fyrsti = sidasti = null;
    else
      fyrsti = fyrsti.naest;
    return res;
  }


  public void put( int i )
  {
    Hlekkur nyr = new Hlekkur();
    n++;
    nyr.tala = i;
    if( sidasti==null )
    f yrsti = sidasti = nyr;
    else
    {
      sidasti.naest = nyr;
      sidasti = nyr;
    }
  }


  public int count()
  {
    return n;
  }

  public static void radixSort(int [] q, int n, int d){
    IntQueue [] queue = new IntQueue[n];

    for (int k = 0; k < n; k++){
      queue[k] = new IntQueue();
    }
    for (int i = d-1; i >=0; i--){
      for (int j = 0; j < n; j++){
        while(queue[j].count() != 0)
        {
          queue[j].get();
        }
      }
      for (int index = 0; index < n; index++){
        // trying to look at one of three digit to sort after.
        int v=1;
        int digit = (q[index]/v)%10;
        v*=10;

        queue[digit].put(q[index]);
      }
      for (int p = 0; p < n; p++){
        while(queue[p].count() != 0) {
          q[p] = (queue[p].get());
        }
      }
    }
  }
}

我也在想,我可以让函数将一个队列作为参数,然后返回该队列的顺序递增吗? 如果可以,怎么办?

请帮忙。 抱歉,如果我的英语不好,那不是很好。

如果您需要更多详细信息,请告知。

import java.util.Random;
public class RadTest extends IntQueue {

    public static void main(String[] args)
    {
        int [] q = new int[10];
        Random r = new Random();
        int t = 0;
        int size = 10;
        while(t != size)
        {
            q[t] = (r.nextInt(1000));
            t++;
        }

        for(int i = 0; i!= size; i++)
        {
            System.out.println(q[i]);
        }

        System.out.println("Radad: \n");
        radixSort(q,size,3);

        for(int i = 0; i!= size; i++)
        {
            System.out.println(q[i]);
        }

    }
}

希望这就是你在说的...


谢谢您的回答,我会调查一下。 不想找人为我解决问题。 寻找帮助和想法,我如何解决。

在我的任务中它说:

为按队列排序的整数实现基数排序功能。 该函数应将一个队列作为参数,并在返回时该队列应按升序包含相同的值。您可以假定这些值在0到999之间。

我可以在队列中放入100个整数并使用radixsort函数对其进行排序吗?还是需要将数字放入数组中,然后将数组放入使用队列的radixsort函数中?

我理解这就像我需要将数字放入Int队列并将该队列放入函数中一样,但这没有用。

但是感谢您的回答,我们会仔细研究它们,并尝试解决我的问题。 但是,如果您认为可以提供帮助,请发表评论。

这适用于我尝试过的测试用例。 它没有完全有据可查,但我认为还可以。 我将它留给您阅读,将其与您当前正在做的事情进行比较,并找出为什么您拥有的东西可能与我的哲学有所不同。 在“懒惰”方式中,还有其他标记在我做它们的地方,您应该做一个更好的方法。

import java.util.*;
class Radix {

    static int[] radixSort(int[] arr) {
        // Bucket is only used in this method, so I declare it here
        // I'm not 100% sure I recommend doing this in production code
        // but it turns out, it's perfectly legal to do!
        class Bucket {
            private List<Integer> list = new LinkedList<Integer>();
            int[] sorted;

            public void add(int i) { list.add(i);  sorted = null;}

            public int[] getSortedArray() {
                if(sorted == null) {
                    sorted = new int[list.size()];
                    int i = 0;
                    for(Integer val : list) {
                        sorted[i++] = val.intValue(); // probably could autobox, oh well
                    }
                    Arrays.sort(sorted); // use whatever method you want to sort here... 
                                         // Arrays.sort probably isn't allowed
                }
                return sorted;
            }
        }

        int maxLen = 0;
        for(int i : arr) {
            if(i < 0) throw new IllegalArgumentException("I don't deal with negative numbers");
            int len = numKeys(i);
            if(len > maxLen) maxLen = len;
        }

        Bucket[] buckets = new Bucket[maxLen];

        for(int i = 0; i < buckets.length; i++) buckets[i] = new Bucket();
        for(int i : arr) buckets[numKeys(i)-1].add(i);

        int[] result = new int[arr.length];
        int[] posarr = new int[buckets.length]; // all int to 0

        for(int i = 0; i < result.length; i++) {
            // get the 'best' element, which will be the most appropriate from
            // the set of earliest unused elements from each bucket
            int best = -1;
            int bestpos = -1;
            for(int p = 0; p < posarr.length; p++) {
                if(posarr[p] == buckets[p].getSortedArray().length) continue;
                int oldbest = best;
                best = bestOf(best, buckets[p].getSortedArray()[posarr[p]]);
                if(best != oldbest) {
                    bestpos = p;
                }


            }
            posarr[bestpos]++;
            result[i] = best;
        }

        return result;

    }

    static int bestOf(int a, int b) {
        if(a == -1) return b;
        // you'll have to write this yourself :)
        String as = a+"";
        String bs = b+"";
        if(as.compareTo(bs) < 0) return a;
        return b;
    }

    static int numKeys(int i) {
        if(i < 0) throw new IllegalArgumentException("I don't deal with negative numbers");
        if(i == 0) return 1;
        //return (i+"").length(); // lame method :}
        int len = 0;
        while(i > 0) {
            len++;
            i /= 10;
        }
        return len;
    }

    public static void main(String[] args) {

        int[] test = {1, 6, 31, 65, 143, 316, 93, 736};
        int[] res = radixSort(test);
        for(int i : res) System.out.println(i);
    }
}

一件事看起来很奇怪:

  for (int p = 0; p < n; p++){
    while(queue[p].count() != 0) {
      q[p] = (queue[p].get());
    }
  }

是p应该是q中从0到n-1的索引,还是队列中从0到9的索引? 不可能两者兼有...

另一个:

  for (int index = 0; index < n; index++){
    // trying to look at one of three digit to sort after.
    int v=1;
    int digit = (q[index]/v)%10;
    v*=10;

    queue[digit].put(q[index]);
  }

为什么将v乘以10,而只在下一次迭代中用v = 1覆盖它? 您是否知道v始终为1,因此每次迭代都将看到相同的数字?

好吧,我认为在几乎不发布解决方案的情况下我无法提供帮助(只是给出提示会比较累人,对不起,我有点累了,对不起),所以我将只做一个不错的模糊测试,以便您可以测试解决方案。 听起来怎么样? :-)

如果要实现某种算法,想出一个好的fuzztester总是一个好主意。 虽然并不能100%地确定它是否会随您的实现而运行(基数排序没有任何奇怪的边缘情况,但我知道这种情况很少发生)

private static void fuzztest() throws Exception{
    Random rnd = new Random();
    int testcnt = 0;
    final int NR_TESTS = 10000;
    // Maximum size of array.
    final int MAX_DATA_LENGTH = 1000;
    // Maximum value allowed for each integer. 
    final int MAX_SIZE = Integer.MAX_VALUE; 

    while(testcnt < NR_TESTS){
     int len = rnd.nextInt(MAX_DATA_LENGTH) + 1;
     Integer[] array = new Integer[len];
     Integer[] radix = new Integer[len];

     for(int i = 0; i < len; i++){
      array[i] = rnd.nextInt(MAX_SIZE);
      radix[i] = new Integer(array[i]);
     }

     Arrays.sort(array);
     sort(radix);  // use your own sort function here.

     for(int i = 0; i < len; i++){
      if(array[i].compareTo(radix[i]) != 0){
       throw new Exception("Not sorted!");
      }
     }
     System.out.println(testcnt);
     testcnt++;
    }        

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM