按升序排列数组元素

3

我正在尝试按升序排序一个数组。出于某些原因它只执行了一次for循环。为什么它不会一直执行直到所有元素都排好序呢?

这是为了完成作业,所以我不能使用现有的排序方法。我应该自己编写该方法。

public class Sudoku {
    public static void main(String[] args) {
        int[] a = { 1, 4, 3, 5, 2 };
        System.out.println(Arrays.toString(sortArray(a)));
    }

    public static int[] sortArray(int[] nonSortedArray) {
        int[] sortedArray = new int[nonSortedArray.length];
        int temp;

        for (int i = 0; i < nonSortedArray.length - 1; i++) {
            if (nonSortedArray[i] > nonSortedArray[i + 1]) {
                temp = nonSortedArray[i];
                nonSortedArray[i] = nonSortedArray[i + 1];
                nonSortedArray[i + 1] = temp;
                sortedArray = nonSortedArray;
            }
        }

        return sortedArray;
    }
}

“由于某种原因,它只执行了一次for循环。”--在你的程序中,你在哪里告诉它要执行多次?程序只会按照你的指示去执行。 - slim
5个回答

4
public static int[] sortArray(int[] nonSortedArray) {
        int[] sortedArray = new int[nonSortedArray.length];
        int temp;
        for (int j = 0; j < nonSortedArray.length - 1; j++) {// added this for loop, think about logic why do we have to add this to make it work

        for (int i = 0; i < nonSortedArray.length - 1; i++) {
            if (nonSortedArray[i] > nonSortedArray[i + 1]) {
                temp = nonSortedArray[i];
                nonSortedArray[i] = nonSortedArray[i + 1];
                nonSortedArray[i + 1] = temp;
                sortedArray = nonSortedArray;

            }
        }
        }
        return sortedArray;
    }

输出:[1, 2, 3, 4, 5]

或者

//making use of j

public static int[] sortArray(int[] nonSortedArray){
    int[] sortedArray = new int[nonSortedArray.length];
    int temp;
    for (int i = 0; i <= nonSortedArray.length; i++) 
    {
        for (int j = i+1; j < nonSortedArray.length; j++)
        {
            if (nonSortedArray[i] > nonSortedArray[j]) 
            {
                temp = nonSortedArray[i];
                nonSortedArray[i] = nonSortedArray[j];
                nonSortedArray[j] = temp;
                sortedArray = nonSortedArray;
            }
        }
    }
    return sortedArray;
}

1
 arr = new int[Item];
  Arrays.sort(arr);

在Java中有内置函数可以对数组进行升序排序。

0

这将按升序对数组进行排序

int arr[]={33,3,4,5,1};
Arrays.sort(arr);
System.out.println(Arrays.toString (arr));

输出将为:[1,3,4,5,33]


0

按升序对数组进行排序

private static int[] sort(int[] array) {
        int[] new_array = new int[array.length];
        int count=0;

        for (int i=0; i<array.length; i++) {
            for(int j=i+1; j<array.length+i;j++) {
                if(array[i]>=array[j%array.length])
                    count++;
            }

            for(int loc=count; loc>0;loc--) {
                if(new_array[loc]==0)
                {
                    new_array[loc]=array[i];
                    break;
                }
            }
            count=0;                            
        }

        return new_array;
    }

0
你正在尝试使用单个循环对一个数组进行排序。为了确保元素按排序顺序排列,需要两个循环。
public static int[] sortArray(int[] nonSortedArray) 
{
    int[] sortedArray = new int[nonSortedArray.length];
    int temp;
    for (int i = 0; i < nonSortedArray.length-1; i++) 
    {
        for (int j = i+1; j < nonSortedArray.length; j++)
        {
            if (nonSortedArray[i] > nonSortedArray[j]) 
            {
                temp = nonSortedArray[i];
                nonSortedArray[i] = nonSortedArray[j];
                nonSortedArray[j] = temp;
                sortedArray = nonSortedArray;
            }
        }
    }
    return sortedArray;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接