Java 数组元素降序排序(非冒泡排序)

说明:每次比较出最大值后,把最大值设置为最小值-1,再次比较该数组

package com.MyJava;

import java.util.Scanner;

public class Test {
    
    
	public static void main(String[] args) {
    
    
		int[] Length = new int[10];
		// 输入10个整数
		int max = 0, min = 0;
		System.out.println("输入" + Length.length + "名同学的身高:");
		for (int i = 0; i < Length.length; i++) {
    
    

			Scanner scan = new Scanner(System.in);
			Length[i] = scan.nextInt();
			// 把第一个输入成绩赋值给max和min,只需要赋值一次!
			if (i == 0) {
    
    
				max = Length[0];
				min = Length[0];
			}
			// 计算最小值
			if (Length[i] < min) {
    
    
				min = Length[i];
			}
		}
		// 计算最大值并依次输出
		System.out.println("身高由高到低的排序为:");
		for (int i = 0; i < Length.length; i++) {
    
    
			int j = 0;
			// 循环计算最大值
			for (j = 0; j < Length.length; j++) {
    
    
				if (Length[j] > max) {
    
    
					max = Length[j];
				}
			}
			System.out.println(max);
			// 找到最大值,并设为最小值,下次循环就能找到更小的值
			for (j = 0; j < Length.length; j++) {
    
    
				if (Length[j] == max) {
    
     // 找到最大值对应的位置j
					Length[j] = min; // 把最大值设为最小值
					max = min; // 设置max为最小值,用于下次循环比较出最大值
				}
			}
		}
	}
}

输入10名同学的身高:
173
175
163
161
185
177
172
188
178
173
身高由高到低的排序为:
188
185
178
177
175
173
173
172
163
161