修改选择排序 Exercise07_20

 1 import java.util.Scanner;
 2 /**
 3  * @author 冰樱梦
 4  * 时间:2018年12月
 5  * 题目:修改选择排序
 6  *
 7  */
 8 public class Exercise07_20 {
 9     public static void main(String[] args){
10         double[] list=new double[10];
11         Scanner input=new Scanner(System.in);
12         System.out.println("输入10个double类型的数: ");
13         for(int i=0;i<list.length;i++){
14             list[i]=input.nextDouble();
15         }
16         SelectionSort(list);
17     }
18     
19     
20     
21     /**
22      * @param 选择排序,原来是找到最小值放到第一位,而后修改程序为找到最大值,放到最后一位
23      */
24     public static void SelectionSort(double list[]){
25         for(int i=list.length-1;i>0;i--){
26             double currentMax=list[i];
27             int currentMaxIndex=i;
28             for(int j=i-1;j>=0;j--){
29                 if(currentMax<list[j]){
30                     currentMax=list[j];
31                     currentMaxIndex=j;
32                 }
33             }
34             if(currentMaxIndex != i){
35                 list[currentMaxIndex]=list[i];
36                 list[i]=currentMax;
37             }
38         }
39         for(double a:list){
40             System.out.print(a+" ");
41         }
42     }
43 }

猜你喜欢

转载自www.cnblogs.com/cherrydream/p/10174154.html