Java 按升序对数组的元素进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33462923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort elements of an array in ascending order
提问by SM360
I'm trying to sort an array in ascending order. For some reason it only performs the for loop once. Why doesn't it keep going until everything is sorted?
我正在尝试按升序对数组进行排序。由于某种原因,它只执行一次 for 循环。为什么它不继续进行,直到一切都被排序?
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;
}
}
采纳答案by RamPrakash
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;
}
回答by Pavan Dittakavi
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;
}
回答by Azmat Ullah
arr = new int[Item];
Arrays.sort(arr);
Built in function in java to sort array in asceding order.
java中内置函数以按升序对数组进行排序。
回答by Vikash kumawat
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]
回答by Valeriu Chirica
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;
}