4

这算哪种排序?感觉都不像,但是最终可以正确排序。

 4 years ago
source link: https://www.oschina.net/question/2507499_2321870
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

这算哪种排序?感觉都不像,但是最终可以正确排序。

颖辉小居 发布于 昨天 14:13
public static void main(String[] args) {
    int[] a = {11, 454, 21, 2, 9};
    bubbleSort(a);
    System.out.println("结果:" + Arrays.toString(a));
}

private static void bubbleSort(int[] a) {
    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < a.length; j++) {
            if (a[i] < a[j]) {
                int temp = a[i];
                a[i] = a[j];
                a[j] = temp;
               System.out.println("i-" + i + " j-" + j + "  " + Arrays.toString(a));
}
        }
    }
}

打印结果:

i-0 j-1  [454, 11, 21, 2, 9]
i-1 j-0  [11, 454, 21, 2, 9]
i-2 j-1  [11, 21, 454, 2, 9]
i-3 j-0  [2, 21, 454, 11, 9]
i-3 j-1  [2, 11, 454, 21, 9]
i-3 j-2  [2, 11, 21, 454, 9]
i-4 j-1  [2, 9, 21, 454, 11]
i-4 j-2  [2, 9, 11, 454, 21]
i-4 j-3  [2, 9, 11, 21, 454]
结果:[2, 9, 11, 21, 454]

看打印不像是冒泡, 看代码不像插入排序


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK