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 发布
本文介绍了一个简单的选择排序算法实现过程,通过定义一个名为selectSort的方法来对整型数组进行排序。该方法首先寻找数组中最小的元素,并将其与数组的第一个元素交换位置;随后继续寻找剩余元素中的最小值并进行交换,以此类推直至整个数组有序。
18万+

被折叠的 条评论
为什么被折叠?



