我有一个作业任务需要手动将数组按升序排序,显然,这需要使用两个for循环来完成:第一个循环遍历现有的数组,并创建一个临时值,其中包括数组的值和索引。第二个循环将临时值与现有值进行比较并排序。我一直试着写代码,但似乎做不对。下面是我最新想出的方法:
我很确定这是不正确的,但如果有人能指点我正确方向,将不胜感激!
public int[] sortArray (int[] inArray)
{
//Construct the array we're using here
int[] newArray = inArray;
for(int x = 0; x < a.length; x++) //a.length = # of indices in the array
{
int tempValue = a[x];
int tempIndex = x;
for(int y = 0; y < a.length; y++)
{
if(tempValue < a[y])
{
newArray[x] = tempValue;
}
}
}
return newArray;
}
我很确定这是不正确的,但如果有人能指点我正确方向,将不胜感激!
a
,但它没有出现在任何地方。请使用inArray
代替。创建新数组的正确方式是int[] newArray = new int[inArray.length];
。 - Petr Janečeka
是我要排序的原始数组。 - Andrew De Forest