Java 中的数组排序
用 Java 编写一个静态方法:
public static void sortByFour (int[] arr)
该方法接收一个充满非负数(零或正数)的数组作为参数,并按以下方式对数组进行排序:
在数组的开头,所有可被整除的数字将出现四。
在它们之后,将出现数组中除以 4 余数为 1 的所有数字。
在在它们之后,将出现数组中除以 4 余数为 2 的所有数字。
在在数组末尾,所有剩余数字(除以 4 余数为 3 的数字)都会出现。
(每组中数字的顺序并不重要。)
该方法必须尽可能高效。
以下是我写的内容,但不幸的是它不能很好地工作... :(
public static void swap( int[] arr, int left, int right )
{
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
public static void sortByFour( int[] arr )
{
int left = 0;
int right = ( arr.length - 1 );
int mid = ( arr.length / 2 );
while ( left < right )
{
if ( ( arr[left] % 4 ) > ( arr[right] % 4 ) )
{
swap( arr, left, right );
right--;
}
if ( ( arr[left] % 4 ) == ( arr[right] % 4 ) )
left++;
else
left++;
}
}
我如何修复或重写我的代码以便它能正常工作?
Write a static method in Java:
public static void sortByFour (int[] arr)
That receives as a parameter an array full of non-negative numbers (zero or positive) and sorts the array in the following way:
In the beginning of the array all the numbers that are divisible by four will appear.
After them all the numbers in the array that divide by 4 with a remainder of 1 will appear.
After them all the numbers in the array that divide by 4 with a remainder of 2 will appear.
In the end of the array all the rest numbers (those which divide by 4 with the remainder 3) will appear.
(The order of the numbers in each group doesn't matter.)
The method must be as efficient as possible.
The following is what I wrote, but unfortunately it doesn't work well... :(
public static void swap( int[] arr, int left, int right )
{
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
public static void sortByFour( int[] arr )
{
int left = 0;
int right = ( arr.length - 1 );
int mid = ( arr.length / 2 );
while ( left < right )
{
if ( ( arr[left] % 4 ) > ( arr[right] % 4 ) )
{
swap( arr, left, right );
right--;
}
if ( ( arr[left] % 4 ) == ( arr[right] % 4 ) )
left++;
else
left++;
}
}
How do I fix or rewrite my code so that it will work well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我将在伪代码中为您执行此操作,但在代码中执行此操作将为您提供答案,这是家庭作业,因此您可以完成自己的工作。 =P
这里是如何在没有列表的情况下做到这一点。此示例根据要求受到限制,但一旦编码即可工作。
然后按各自的顺序连接四个桶以获得最终结果
I will do this in psuedocode for you but doing it in code would be giving you the answer and this is homework so you can do your own work. =P
Here is how to do it without lists. This example is restricted based on the requirements but will work once you code it.
then concatenate the four buckets in their respective order to get your end result
您可以这样解决这个问题:
if A[i] > A[i+1] 然后...
。if myIsGreater(A[i], A[i+1]) then...
)。对于步骤 3,通过考虑模运算符,您肯定走在正确的轨道上。
You can attack the problem like this:
if A[i] > A[i+1] then...
.if myIsGreater(A[i], A[i+1]) then...
).For step 3, you are definitely on the right track by considering the modulus operator.
我会重申丹尼尔所说的话:你可以选择排序算法。如果需要的话,请使用迄今为止在课程中介绍过的最有效的方法。但是,当您在算法中比较两个项目时,请比较它们的值 mod 4。所以类似于
if A[i] >如果 A[i] % 4 > 则 A[j]
变为如果 A[j] % 4 。然后,您继续执行算法指定的对该数组元素执行的操作。交换、冒泡、退货,无论需要什么。
关于您发布的代码,我认为它不适合快速修复。它应该使用哪种算法?
mid
是做什么用的?我认为,一旦您知道打算使用哪种算法,并找出哪一行代码包含比较,您将能够快速查看并编写解决方案。一般来说,对于这类事情,如果您在担心效率之前专注于获得可行的解决方案,这会有所帮助。第一次遇到这样的问题时,我使用了选择排序。我建议先尝试一下,然后利用获得的经验进行归并排序,时间复杂度为 O(n log n)。
I will reiterate what Daniel said: you have your choice of sorting algorithm. Use the most efficient one that you've covered in the class so far, if that is the requirement. But when you get to the point in your algorithm where two items are being compared, compare their value mod 4 instead. So something like
if A[i] > A[j]
becomesif A[i] % 4 > if A[j] % 4
. Then you continue doing whatever the algorithm specifies that you do with that array element. Swap it, bubble it up, return it, whatever is called for.Regarding the code that you posted, I don't think that it lends itself to a quick fix. Which algorithm is it supposed to be using? What is
mid
there for? I think that once you know which algorithm you intend to use, and figure out which line of code contains the comparison, you will be able to quickly see and code a solution.In general, with these sorts of things, it can help if you focus on getting a working solution before you worry about efficiency. I used selection sort the first time I had to do a problem like this. I would recommend trying that first, and then using the experience gained to do one with merge sort, which is O(n log n).
线性时间解决方案
最渐进有效的方法是使用桶排序。
因此,这在
O(N)
中对数字进行排序,这是最优的。这里的关键是,通过对以 4 为模的数字进行排序,实际上只有 4 个数字需要排序:0、1、2、3。List
的说明性解决方案下面是上述算法的实现(对于一般模
M
) 使用 为了清楚起见,List
和 for-each 。忽略未经检查的强制转换警告,只专注于理解算法。一旦您完全理解了该算法,将其转换为使用数组(如果必须的话)就很简单了。
另请参见
java.util.List;
API - “有序集合”add(E e)
- “将指定元素附加到此列表的末尾”addAll(...)
- “将指定集合中的所有元素追加到此列表的末尾”关于
%
的特别说明数字非负的规定很重要,因为
%
不是数学定义的取模运算符;它是余数运算符。参考文献
A linear-time solution
The most asymptotically efficient way to do this would be to use bucket sort.
Thus, this sorts the numbers in
O(N)
, which is optimal. The key here is that by sorting on numbers modulo 4, there are essentially only 4 numbers to sort: 0, 1, 2, 3.An illustrative solution with
List
Here's an implementation of the above algorithm (for general modulo
M
) usingList
and for-each for clarity. Ignore the unchecked cast warning, just concentrate on understanding the algorithm.Once you fully understand the algorithm, translating this to use arrays (if you must) is trivial.
See also
java.util.List<E>
API - "An ordered collection"add(E e)
- "Appends the specified element to the end of this list"addAll(...)
- "Appends all of the elements in the specified collection to the end of this list"A special note on
%
The stipulation that numbers are non-negative is significant, because
%
is NOT the modulo operator as it's mathematically defined; it's the remainder operator.References
阅读此页。 http://en.wikipedia.org/wiki/Sorting_algorithm#Summaries_of_popular_sorting_algorithms
这应该有帮助从长远来看你是最好的。也挺好玩的!
Read this page. http://en.wikipedia.org/wiki/Sorting_algorithm#Summaries_of_popular_sorting_algorithms
That should help you best in the long run. It's also pretty fun!
这是一种 Java 式的方式......
Here's a Java-ish way...