Java 基数排序

发布于 2024-08-11 08:34:22 字数 1214 浏览 6 评论 0原文

欢迎。我有一个基数排序方法,它使用一个数组来遍历,但必须有另一个数组(bin)来存储在一个空队列中。我很困惑如何为垃圾箱排队。我还有一个 findPlace 方法,可以在调用时找到每个数字的位置。所以,这就是我得到的。有人可以帮我找到我缺少的东西吗?非常感谢您抽出时间。

public static void radix(int [] list){
       int [] bin = new int[10]; 
      ArrayQueue<Integer> part = new ArrayQueue<Integer>(); // EDIT What would I do with this queue?? 
      int num = 0; 
         for(int i=0;i<list.length;i++)
         {
            bin[i] = 0;  
          }

            for(int pass=0;pass<list.length;pass++)
           {
                for(int num=0;num<list.length;num++)
               {
                       int digit=findPlace(bin[pass], num);
                 }

                  bin[digit].add(list[num]); // add to the bin
            }
              // Put back into list
               for(int h=0; h<10; h++)
               {
                    while(!bin[h].isEmpty())
                    {
                       list[num] = bin[queueNum].remove();
                     num++;
                    }
              }
        }


public static int getPlace (int x, int place)
    {return x/place % 10;}

我还制作了一个方法来查找存储桶,所以我只需要知道如何将其放入数组中,我会这样做吗?部分.add(getPlace(x, 地点));?

Welcome. I have a radix sorting method that uses an array to go through, but has to have another array (bin) that will store in an empty queue. I am confused as to how I would make a queue for the bins. I also have a findPlace method that finds the place of the each digit when called upon. So, here is what I got. Can someone help me find what I am missing? Thanks so much for your time.

public static void radix(int [] list){
       int [] bin = new int[10]; 
      ArrayQueue<Integer> part = new ArrayQueue<Integer>(); // EDIT What would I do with this queue?? 
      int num = 0; 
         for(int i=0;i<list.length;i++)
         {
            bin[i] = 0;  
          }

            for(int pass=0;pass<list.length;pass++)
           {
                for(int num=0;num<list.length;num++)
               {
                       int digit=findPlace(bin[pass], num);
                 }

                  bin[digit].add(list[num]); // add to the bin
            }
              // Put back into list
               for(int h=0; h<10; h++)
               {
                    while(!bin[h].isEmpty())
                    {
                       list[num] = bin[queueNum].remove();
                     num++;
                    }
              }
        }


public static int getPlace (int x, int place)
    {return x/place % 10;}

I also made a method to find the bucket, So i just need to know how I would put it into an array, would I just do this? part.add(getPlace(x, place));?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少年亿悲伤 2024-08-18 08:34:22

您的数组 bin 不会仅仅因为您希望它像队列一样工作:) 数组没有像 add()remove() 这样的方法。关于如何解决此问题,您有两种选择:

  • 自行编写正确处理队列的程序:队列由一个数组和两个指向该数组的指针组成,传统上称为 headtail。您必须编写自己的方法来添加和删除,这些方法将与数组和指针一起使用,并处理空队列或溢出队列。

  • 使用Java的内置Queue类。它记录在库的 Javadocs 中。不过,不知道您的作业是否打算让您构建自己的队列。

更新另一个细节:

您询问如何从数字中提取单个数字。正如维基百科文章中所建议的,从最低有效数字 (LSD) 开始工作是最简单的。为此:

  • 要提取最后一位数字,请执行 digit = number % 10 (这是模运算)。您将得到一个 0 到 9 之间的整数(含 0 和 9)。
  • 要去掉最后一位数字,只需除以 10。然后你就可以去掉另一个数字。
  • 由于您需要多次查看数字的倒数第 n 个数字,因此最好将此功能放入单独的方法中。

您可以使用 0 到 9 选择正确的队列来放置您的号码。

当您将所有号码排入 10 个存储桶后,您需要将它们从那里复制回单个列表中。然后,只要任何数字中仍有未处理的数字,就重复此操作。

Your array, bin doesn't act like a queue just because you want it to :) Arrays don't have methods like add() and remove(). You have two choices on how to fix this:

  • Program in the proper handling for queues yourself: A queue consists of an array and two pointers into that array, traditionally called head and tail. You'd have to write your own methods to add and remove, which would work with the array and the pointers and take care of an empty queue or an overflowing one.

  • Use Java's built-in Queue class. It's documented in the library's Javadocs. Don't know if your assignment intends for you to build your own queue, though.

Update with another detail:

You asked how to extract a single digit from a number. It's easiest working from the least significant digits (LSD) as suggested in the Wikipedia article. To do that:

  • To extract the last digit, do digit = number % 10 (that's the modulo operation). You will get an integer between 0 and 9 inclusive.
  • To strip off the last digit, simply divide by 10. Then you can pull off another digit.
  • Since you'll need to be looking at the n-th last digit of a number several times, you would do well to put this functionality into a separate method.

You can use your 0 thru 9 to select the right queue to put your number on.

When you've finished queueing all your numbers into the 10 buckets, you need to copy them from there back into a single list. Then repeat as long as there are still digits in any number that you haven't processed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文