冒泡排序

1.冒泡排序:

时间复杂度:O(n^2)

空间复杂度:O(1)

2.Java代码如下:

public static void main(String[] args) {
    int[] arr = {1, 23, 453, 12, 13, 46, 87, 78, 23, 21};
    int[] res = Quit3(arr);
    print(res);
}

private static int[] Quit3(int[] arr) {
    int length = arr.length;
    for (int i = 0; i < length; i++) {
        int tmp;
        for (int j = 0; j < length - i - 1; j++) {
            if (arr[j + 1] < arr[j]) {
                tmp = arr[j + 1];
                arr[j + 1] = arr[j];
                arr[j] = tmp;
            }
        }
    }
    return arr;
}

private static void print(int[] arr) {
    int length = arr.length;
    for (int i = 0; i < length; i++) {
        System.out.println(arr[i]);
    }
}
posted @ 2016-11-26 22:23  Thomas_188  阅读(103)  评论(0编辑  收藏  举报