MCPcopy Index your code
hub / github.com/hustcc/JS-Sorting-Algorithm / BubbleSort

Class BubbleSort

src/java/main/BubbleSort.java:6–33  ·  view source on GitHub ↗

冒泡排序

Source from the content-addressed store, hash-verified

4 * 冒泡排序
5 */
6public class BubbleSort implements IArraySort {
7
8 @Override
9 public int[] sort(int[] sourceArray) throws Exception {
10 // 对 arr 进行拷贝,不改变参数内容
11 int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);
12
13 for (int i = 1; i < arr.length; i++) {
14 // 设定一个标记,若为true,则表示此次循环没有进行交换,也就是待排序列已经有序,排序已经完成。
15 boolean flag = true;
16
17 for (int j = 0; j < arr.length - i; j++) {
18 if (arr[j] > arr[j + 1]) {
19 int tmp = arr[j];
20 arr[j] = arr[j + 1];
21 arr[j + 1] = tmp;
22
23 flag = false;
24 }
25 }
26
27 if (flag) {
28 break;
29 }
30 }
31 return arr;
32 }
33}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected