public class selectSort {
public static void selectSort(int [] arr){
int min;
for(int x = 0; x < arr.length - 1; x++){
min = x;
for(int y = x+1;y < arr.length;y++){
if (arr[min] > arr[y])
min = y;
}
swap(x, min, arr);
}
}
private static void swap(int x, int min, int [] a) {
int temp = a[min];
a[min] = a[x];
a[x] = temp;
}
public static void main(String [] args){
int [] arr = {9,8,7,6,5,4,3,2,1};
selectSort(arr);
for(int y = 0;y < arr.length; y++){
System.out.print(arr[y] + ",");
}
}
}
选择排序
最新推荐文章于 2024-09-28 22:14:04 发布