import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Arrays;
public class SortPractice {
private int[] railgun = {2,7,4,2,4,7,1};
public SortPractice() {
System.out.println(Arrays.toString(railgun));
SelectionSort(railgun);
System.out.println(Arrays.toString(railgun));
}
public static void SelectionSort(int[] ray){
for(int j = 0; j < ray.length; j++){
int low = 0;
for(int i = j; i < ray.length;i++){
if(ray[i] < ray[low])
low = i;
}
System.out.println(ray[low]);
int temp = ray[low];
ray[low] = ray[j];
ray[j] = temp;
}
}
public static void main(String[] args) {
SortPractice steve = new SortPractice();
}
}
我已经将这些代码写在纸上并经过多次运行,但由于某些原因它无效。我不确定我是否错过了-1或者其他东西并且没有注意到。有人请看看。
我不是在找工作代码。只是和解释/修复我的所以我可以更好地理解我做错了什么以及如何自己解决它。谢谢:))
答案 0 :(得分:1)
这里可能有问题:int low = 0; 尝试改变low = j
我试过,它有效
public static void SelectionSort(int[] ray){
for(int j = 0; j < ray.length; j++){
int low = j;
for(int i = j; i < ray.length;i++){
if(ray[i] < ray[low])
low = i;
}
System.out.println(ray[low]);
int temp = ray[low];
ray[low] = ray[j];
ray[j] = temp;
}
答案 1 :(得分:0)
试试这个。很简单。
public static void SelectionSort(int[] numbers) {
int tempVar = 0;
for (int i = 0; i < numbers.length; i++) {
for (int j = 1; j < numbers.length - i; j++) {
if (numbers[j - 1] > numbers[j]) {
tempVar = numbers[j - 1];
numbers[j - 1] = numbers[j];
numbers[j] = tempVar;
}
}
}
}
答案 2 :(得分:0)
int low = j;
否则你总是将数字与第一个数字进行比较。
注意:
不要说你只是一个初学者。这些是每个人都应该首先学习的问题。