Java 冒泡排序

1585364631
2023-02-20 / 0 评论 / 108 阅读 / 正在检测是否收录...

Java 冒泡排序

    public static void main(String[] args) {
        int[] a = {5,4,2,1,3,6,8,9,10};
        for (int i = 0; i < a.length - 1; i++) {
            for (int ii = 0;ii < a.length - 1 - i;ii++){
                if (a[ii] > a[ii+1]){
                    a[ii] = a[ii] ^ a[ii+1];
                    a[ii+1] = a[ii] ^ a[ii+1];
                    a[ii] = a[ii] ^ a[ii+1];
                }
            }
        }
        System.out.println(Arrays.toString(a));
    }
    }

结果

[1, 2, 3, 4, 5, 6, 8, 9, 10]

0

评论 (0)

取消