Skip to content

Latest commit

 

History

History
42 lines (30 loc) · 848 Bytes

13. BubbleSort.md

File metadata and controls

42 lines (30 loc) · 848 Bytes

13. BubbleSort

package com.alexanderli95.algorithm;

//冒泡排序1
public class BubbleSort {


    public static void main(String[] args) {

        int[] data={9,8,7,6,5,4,3,2,1};
        boolean sorted=true;

        int times=data.length;
        for(int j=0;j<data.length;j++) {
            sorted=true;
            for (int i = 0; i < times-1; i++) {
                if (data[i] > data[i + 1]) {
                    int temp = data[i];
                    data[i] = data[i + 1];
                    data[i + 1] = temp;
                    sorted=false;
                }
            }

            if(sorted){
                break;
            }

            times--;
        }

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

}