public class Paixu {
public static void main(String[] args) {
int[] a = new int[5];
System.out.print("排序前: ");
for(int i = 0; i < a.length; i++) {
a[i] = (int) (Math.random() * 99 + 1);
System.out.print(a[i] + " ");
}
// 简单选择排序
for(int i = 0; i < a.length - 1; i++) {
int minIndex = i;
for(int j = i + 1; j < a.length; j++) {
if(a[j] < a[minIndex]) {
minIndex = j;
}
}
if(minIndex != i) {
int temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
}
System.out.print("\n排序后: ");
for(int x : a) {
System.out.print(x + " ");
}
}
}
简单选择排序
最新推荐文章于 2023-03-20 01:02:49 发布