Discuss / Java / 练习

练习

Topic source

鹤辞

#1 Created at ... [Delete] [Delete and Lock User]
/**
	 * 
	 * @Title:         bubbleSort   
	 * @Description:   排序
	 * @param:         @param ns 待排序数组
	 * @param:         @param ascend 是否升序
	 * @return:        void   
	 * @author:        清风  
	 * @date:          2022年9月26日 下午2:11:54   
	 * @throws
	 */
	public static void bubbleSort(int[] ns, Boolean ascend) {
		System.out.println(Arrays.toString(ns));
		for (int i = 0; i < ns.length - 1; i++) {
			if (ascend) {
				for (int j = 0; j < ns.length - i - 1; j++) {
					if (ns[j] > ns[j + 1]) {
						int tmp = ns[j];
						ns[j] = ns[j + 1];
						ns[j + 1] = tmp;
					}
				}
			} else {
				for (int j = ns.length - 1; j > i; j--) {
					if (ns[j - 1] < ns[j]) {
						int tmp = ns[j - 1];
						ns[j - 1] = ns[j];
						ns[j] = tmp;
					}
				}
			}
		}
		System.out.println(Arrays.toString(ns));
	}

Æ

#2 Created at ... [Delete] [Delete and Lock User]

import java.util.*;

public class Main { 

    public static void main(String[] args) {

        Integer [] ns = { 28, 12, 89, 73, 65, 18, 96, 50, 8, 36 };

        Arrays.sort(ns,Collections.reverseOrder());

        int a=11;

        System.out.println(Arrays.toString(ns));

    }

}

好像找不到回复界面   借用一下


  • 1

Reply