Java 中冒泡排序

package com.nf147.test;

public class sort {
    public static void main(String[] args) {

        int arr[] = {11, 1, 3, 10, 7, 5, 20, 34, 14, 16};


        System.out.println("排序前");

        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
        System.out.println("==================");

        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if(arr[j]>arr[j+1]){
                    int tem=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=tem;
                }
            }
        }


        for (int num : arr) {
            System.out.print(num + " ");
        }


    }
}

 

 

posted @ 2019-03-26 23:14  随★风  阅读(167)  评论(0编辑  收藏  举报