Java实现BubbleSort

public class BubbleSort {
    public static void sort(int a[]){
        for(int j = a.length - 1 ;j > 0;j--){
            for(int i = 0; i < j; i++){
                if(a[i+1] < a[i] ){
                    int tmp = a[i];
                    a[i] = a[i+1];
                    a[i+1] = tmp;
                }
            }
        }

        for(int x : a){
            System.out.print(x + " ");

        }
    }

    public static void main(String[] args){
        int a[] = {8,9,3,4,5,2,-1,1,61,3,6,0,9,8,7,4};
        sort(a);
    }

猜你喜欢

转载自blog.csdn.net/qq_18287147/article/details/106052533