我正在尝试制作一个用于对随机数组进行排序的应用程序 我编写了一些代码,但我看不出它有什么问题,它返回了错误的值 备注:我正在努力学习编程。因此,不要提出解决问题的完全不同的方法。我只是想看看这段代码有什么问题,这样我就可以做得更好。 RandomArrayCreator.create() 返回的只是一个随机顺序的数字数组。
public class ArraySorter
{
public static void main(String[] args)
{
int[] siyahi = RandomArrayCreator.create();
int[] siralanmish = new int[siyahi.length];
for (int i=0;i<siyahi.length;i++)
{
for (int j=0;j<siyahi.length;j++)
{
for (int k=j+1;k<siyahi.length;k++)
{
if (siyahi[k]<siyahi[j]) j=k;
}
siralanmish[i]=siyahi[j];
siyahi[j]=siyahi.length+1;
}
System.out.println(siralanmish[i]);
}
}
}
最佳答案
我知道您不想要建议,但无论如何我都会提供一个。
希望这能帮助指导您,但仍然允许您提出自己的解决方案。
从小到大排序。
did I have swap an element?
while I swapped an element
assume I did not swap an element
for element i in the array
is i > i+1?
if yes
swap the elements
I did swap an element
else
do nothing
关于java - 在 Java 中对随机数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22623557/