验证中...
Your message has been delivered
Languages: Java
Categories: 算法分析
Latest update 2018-01-13 23:19
选择排序
Raw Copy
public class SelectionSort {
public static void main(String[] args) {
int[] arr={12, 10, 11, 7, 6, 5, 4, 3, 2, 1};
System.out.println("交换之前:");
for(int num:arr){
System.out.print(num+" ");
}
//选择排序的优化
for(int i = 0; i < arr.length - 1; i++) {// 做第i趟排序
int k = i;
for(int j = k + 1; j < arr.length; j++){// 选最小的记录
if(arr[j] < arr[k]){
k = j; //记下目前找到的最小值所在的位置
}
}
//在内层循环结束,也就是找到本轮循环的最小的数以后,再进行交换
if(i != k){ //交换a[i]和a[k]
int temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
}
System.out.println();
System.out.println("交换后:");
for(int num:arr){
System.out.print(num+" ");
}
}
}

Comment list( 0 )

You need to Sign in for post a comment

6_float_left_people 6_float_left_close