选择排序 发表于 2019-04-29 | 更新于 2019-05-02 | 分类于 算法 | 评论数: | 阅读次数: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103public class SelectSortDemo { public static void main(String[] args) { // 定义一个数组 int[] arr = {24, 69, 80, 57, 13} ; // 遍历方法 System.out.print("排序前: "); BubbleSortDemo.print(arr) ; // 选择排序 selectSort2(arr) ; // 排序后的输出 System.out.print("排序后: "); BubbleSortDemo.print(arr) ; } /** * 选择排序 */ private static void selectSort2(int[] arr) { for(int index = 0 ; index < arr.length - 1 ; index++){ for(int x = 1 + index ; x < arr.length ; x++){ if(arr[x] < arr[index]){ int temp = arr[x] ; arr[x] = arr[index] ; arr[index] = temp ; } } } } /** * 选择排序的推导过程 */ private static void selectSort(int[] arr) { // 第一次排序 // 定义一个int类型的变量 int index = 0 ; // 循环 for(int x = 1 + index ; x < arr.length ; x++){ if(arr[x] < arr[index]){ int temp = arr[x] ; arr[x] = arr[index] ; arr[index] = temp ; } } // 第二次排序 index = 1 ; // 循环 for(int x = 1 + index ; x < arr.length ; x++){ if(arr[x] < arr[index]){ int temp = arr[x] ; arr[x] = arr[index] ; arr[index] = temp ; } } // 第三次排序 index = 2 ; // 循环 for(int x = 1 + index ; x < arr.length ; x++){ if(arr[x] < arr[index]){ int temp = arr[x] ; arr[x] = arr[index] ; arr[index] = temp ; } } // 第四次排序 index = 3 ; // 循环 for(int x = 1 + index ; x < arr.length ; x++){ if(arr[x] < arr[index]){ int temp = arr[x] ; arr[x] = arr[index] ; arr[index] = temp ; } } } } -------------本文结束感谢您的阅读------------- 打赏 微信支付 支付宝