java 自己实现 冒泡 排序 demo

2017-07-06

34

1

技术:java8

运行环境:IDEA 15.2 + jdk8 + windows 10

demo功能:java实现一个冒泡排序

源代码

http://git.oschina.net/youlixishi/demo-world/blob/master/src/algorithm/binary-search/src/main/java/com/demoworld/BubbleSortTest.java

public void bubbleSort(int[] arr) {
    if (arr == null || arr.length == 0) {
        return;
    }
    while (true) {
        boolean finished = true;
        for (int i = 0; i <= arr.length - 1; i++) {
            if (i == arr.length - 1) {
                break;
            }
            if (arr[i] > arr[i + 1]) {
                int max = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = max;
                finished = false;
            }
        }
        if (finished) {
            break;
        }
    }

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

欢迎添加微信,互相学习↑↑↑ -_-

发表评论

全部评论:0条

白老虎

programming is not only to solve problems, ways to think