HW6.17

时间:2021-06-10 03:58:48

HW6.17

 import java.util.Scanner;

 public class Solution
 {
     public static void main(String[] args)
     {
         Scanner input = new Scanner(System.in);
         System.out.print("Enter count of numbers: ");
         int count = input.nextInt();
         double[] number = new double[count];
         for(int i = 0; i < count; i++)
             number[i] = input.nextDouble();

         input.close();

         for(double i: number)
             System.out.print(i + " ");
         System.out.println();
         selectSort(number);
         for(double i: number)
             System.out.print(i + " ");
     }

     public static void selectSort(double[] array)
     {
         double maxValue, temp;
         int index;
         for(int i = array.length - 1; i > 0; i--)
         {
             maxValue = array[i];
             index = i;
             for(int j = i - 1; j >= 0; j--)
                 if(array[j] > maxValue)
                 {
                     maxValue = array[j];
                     index = j;
                 }
             if(maxValue > array[i])
             {
                 temp = array[i];
                 array[i] = array[index];
                 array[index] = temp;
             }
         }
     }
 }

相关文章