MCPcopy Create free account
hub / github.com/algorithmzuo/algorithm-journey / bubbleSort

Method bubbleSort

src/class007/Complexity.java:9–26  ·  view source on GitHub ↗
(int[] arr)

Source from the content-addressed store, hash-verified

7 // 只用一个循环完成冒泡排序
8 // 但这是时间复杂度O(N^2)的!
9 public static void bubbleSort(int[] arr) {
10 if (arr == null || arr.length < 2) {
11 return;
12 }
13 int n = arr.length;
14 int end = n - 1, i = 0;
15 while (end > 0) {
16 if (arr[i] > arr[i + 1]) {
17 swap(arr, i, i + 1);
18 }
19 if (i < end - 1) {
20 i++;
21 } else {
22 end--;
23 i = 0;
24 }
25 }
26 }
27
28 public static void swap(int[] arr, int i, int j) {
29 int tmp = arr[i];

Callers 1

mainMethod · 0.95

Calls 1

swapMethod · 0.95

Tested by

no test coverage detected