我正在尝试创建一个选择排序方法来对 arraylist num 进行排序,但它对我不起作用。您认为我的内容有问题吗?它只是按照添加项目的顺序返回数组列表。
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
ArrayList<Integer> num = new ArrayList<Integer>();
num.add(5);
num.add(6);
num.add(3);
num.add(2);
selectionSort(num);
System.out.println(num);
}
static void selectionSort(ArrayList<Integer> num){
int n = num.size();
for(int i = 0; i < n-1; i++ ){
int min = i;
for(int j = i+1; j < n; j++){
if(num.get(j) < num.get(min)){
min = j;
}
}
int temp = num.get(min);
num.set(min, num.get(i));
num.set(i, temp);
}
}
}
最佳答案
刚刚意识到我需要返回数组。将来会留给有类似问题的人。
关于java - 选择排序未正确排序 - 或根本没有排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58308882/