简体   繁体   English

以升序对数组的元素进行排序

[英]Sort elements of an array in ascending order

I'm trying to sort an array in ascending order. 我正在尝试按升序对数组进行排序。 For some reason it only performs the for loop once. 由于某种原因,它仅执行一次for循环。 Why doesn't it keep going until everything is sorted? 为什么直到一切都整理好之后它才继续前进?

It's for an assignment so I am not allowed to use existing sort methods. 它用于分配作业,因此不允许使用现有的排序方法。 I'm supposed to write the method myself. 我应该自己写方法。

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;
    }
}
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;
    }

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

or 要么

//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;
}
 arr = new int[Item];
  Arrays.sort(arr);

Built in function in java to sort array in asceding order. Java中的内置函数可按升序对数组进行排序。

You are trying to sort an array using a single loop. 您正在尝试使用单个循环对数组进行排序。 You would be needing two loops to ensure that the elements are in sorted order. 您将需要两个循环来确保元素按排序顺序。

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;
}

This will sort an array in ascending order 这将以升序对数组进行排序

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

output will:-[1,3,4,5,33] 输出将为:-[1、3、4、5、33]

Sorting array in ascending order 升序排列数组

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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM