java中的bubbleSort算法

package com.lut.javase.array;

public class BubbleSort {
    public static void main(String[] args) {
        //每一次都泡泡出一个最大值放至最后
        /*
        * 例如 数组{7, 4, 5, 6, 7, 9, 0}*/
        int[] array = {7, 4, 5, 6, 7, 9, 0};
        for(int i = array.length - 1; i > 0; i--){
            for(int j = 0; j < i; j++){
                if(array[j] > array[j+1]){
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;

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

    }
}

 

posted @ 2022-07-05 18:10  _八级大狂风  阅读(47)  评论(0编辑  收藏  举报