算法排序的思想
[编程语言教程]

先放一段冒泡排序的代码

public class Test {
    public void bubbleSort(int[] a){
        if (a.length<=1){return;}
        for (int i = 0; i < a.length; i++) {
            boolean flag = false;
            for (int j = 0; j < a.length - i - 1; j++) {
                int tmp = a[j];
                if (a[j]>a[j+1]){
                    a[j]=a[j+1];
                    a[j+1] = tmp;
                    flag = true;
                }
            }
            if (flag==false){
                return;
            }
        }
    }

    public static void main(String[] args) {
        int[] q = {10,20,3,2,4,6,7,5};
        Test test = new Test();
        test.bubbleSort(q);
        System.out.println(Arrays.toString(q));
    }
}
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » 算法排序的思想