Java中的选择排序。
以下是必需的程序。
示例
public class Tester {
public static void selectionSort(int[] arr){
for (int i = 0; i < arr.length - 1; i++){
int index = i;
for (int j = i + 1; j < arr.length; j++){
if (arr[j] < arr[index]){
index = j;
}
}
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}
public static void main(String a[]){
int arr[] = {21,60,32,01,41,34,5};
System.out.println("Selection Sort");
for(int i:arr){
System.out.print(i+" ");
}
System.out.println();
selectionSort(arr);
System.out.println("Selection Sort");
for(int i:arr){
System.out.print(i+" ");
}
}
}输出结果
Before Selection Sort 21 60 32 1 41 34 5 After Selection Sort 1 5 21 32 34 41 60
热门推荐
10 新婚祝福语大全简短经典
11 打游戏通关祝福语简短
12 冬季祝福语大全 简短的
13 孩子满月随礼简短祝福语
14 叔叔生日祝福语简短搞笑
15 舅舅大婚文案祝福语简短
16 感动妈妈的祝福语简短
17 新人结婚简短祝福语大全
18 对别人新年祝福语简短