|
| 1 | +# 常见的排序算法 |
| 2 | + |
| 3 | +## 冒泡排序 |
| 4 | + |
| 5 | +定义一个布尔变量 `hasChange`,用来标记每轮是否进行了交换。在每轮遍历开始时,将 `hasChange` 设置为 false。 |
| 6 | + |
| 7 | +### 代码示例 |
| 8 | + |
| 9 | +```java |
| 10 | +import java.util.Arrays; |
| 11 | + |
| 12 | +public class BubbleSort { |
| 13 | + private static void swap(int[] nums, int i, int j) { |
| 14 | + int t = nums[i]; |
| 15 | + nums[i] = nums[j]; |
| 16 | + nums[j] = t; |
| 17 | + } |
| 18 | + |
| 19 | + private static void bubbleSort(int[] nums) { |
| 20 | + boolean hasChange = true; |
| 21 | + for (int i = 0; i < nums.length - 1 && hasChange; ++i) { |
| 22 | + hasChange = false; |
| 23 | + for (int j = 0; j < nums.length - 1 - i; ++j) { |
| 24 | + if (nums[j] > nums[j + 1]) { |
| 25 | + swap(nums, j, j + 1); |
| 26 | + hasChange = true; |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public static void main(String[] args) { |
| 33 | + int[] nums = {1, 2, 7, 9, 5, 8}; |
| 34 | + bubbleSort(nums); |
| 35 | + System.out.println(Arrays.toString(nums)); |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +### 算法分析 |
| 41 | + |
| 42 | +空间复杂度 O(1)、时间复杂度 O(n^2)。 |
| 43 | + |
| 44 | +分情况讨论: |
| 45 | + |
| 46 | +1. 给定的数组按照顺序已经排好:只需要进行 `n-1` 次比较,两两交换次数为 0,时间复杂度为 O(n),这是最好的情况。 |
| 47 | +2. 给定的数组按照逆序排列:需要进行 `n*(n-1)/2` 次比较,时间复杂度为 O(n^2),这是最坏的情况。 |
| 48 | +3. 给定的数组杂乱无章。在这种情况下,平均时间复杂度 O(n^2)。 |
| 49 | + |
| 50 | +因此,时间复杂度是 O(n^2),这是一种稳定的排序算法。 |
| 51 | + |
| 52 | +> 稳定是指,两个相等的数,在排序过后,相对位置保持不变。 |
| 53 | +
|
| 54 | +## 插入排序 |
| 55 | + |
| 56 | +与冒泡排序对比: |
| 57 | + |
| 58 | +- 在冒泡排序中,经过每一轮的排序处理后,数组后端的数是排好序的。 |
| 59 | +- 在插入排序中,经过每一轮的排序处理后,数组前端的数是排好序的。 |
| 60 | + |
| 61 | +插入排序的算法思想是:不断将尚未排好序的数插入到已经排好序的部分。 |
| 62 | + |
| 63 | +### 代码示例 |
| 64 | + |
| 65 | +```java |
| 66 | +import java.util.Arrays; |
| 67 | + |
| 68 | +public class InsertionSort { |
| 69 | + private static void insertionSort(int[] nums) { |
| 70 | + for (int i = 1, j, current; i < nums.length; ++i) { |
| 71 | + current = nums[i]; |
| 72 | + for (j = i - 1; j >= 0 && nums[j] > current; --j) { |
| 73 | + nums[j + 1] = nums[j]; |
| 74 | + } |
| 75 | + nums[j + 1] = current; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public static void main(String[] args) { |
| 80 | + int[] nums = { 1, 2, 7, 9, 5, 8 }; |
| 81 | + insertionSort(nums); |
| 82 | + System.out.println(Arrays.toString(nums)); |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +### 算法分析 |
| 88 | + |
| 89 | +空间复杂度 O(1),时间复杂度 O(n^2)。 |
| 90 | + |
| 91 | +分情况讨论: |
| 92 | + |
| 93 | +1. 给定的数组按照顺序排好序:只需要进行 n-1 次比较,两两交换次数为 0,时间复杂度为 O(n),这是最好的情况。 |
| 94 | +2. 给定的数组按照逆序排列:需要进行 `n*(n-1)/2` 次比较,时间复杂度为 O(n^2),这是最坏的情况。 |
| 95 | +3. 给定的数组杂乱无章:在这种情况下,平均时间复杂度是 O(n^2)。 |
| 96 | + |
| 97 | +因此,时间复杂度是 O(n^2),这也是一种稳定的排序算法。 |
| 98 | + |
| 99 | +## 归并排序 |
| 100 | + |
| 101 | +归并排序的核心思想是分治,把一个复杂问题拆分成若干个子问题来求解。 |
| 102 | + |
| 103 | +归并排序的算法思想是:把数组从中间划分为两个子数组,一直递归地把子数组划分成更小的数组,直到子数组里面只有一个元素的时候开始排序。排序的方法就是按照大小顺序合并两个元素。接着依次按照递归的顺序返回,不断合并排好序的数组,直到把整个数组排好序。 |
| 104 | + |
| 105 | +### 代码示例 |
| 106 | + |
| 107 | +```java |
| 108 | +import java.util.Arrays; |
| 109 | + |
| 110 | +public class MergeSort { |
| 111 | + |
| 112 | + private static void merge(int[] nums, int low, int mid, int high, int[] temp) { |
| 113 | + int i = low, j = mid + 1, k = low; |
| 114 | + while (k <= high) { |
| 115 | + if (i > mid) { |
| 116 | + temp[k++] = nums[j++]; |
| 117 | + } else if (j > high) { |
| 118 | + temp[k++] = nums[i++]; |
| 119 | + } else if (nums[i] <= nums[j]) { |
| 120 | + temp[k++] = nums[i++]; |
| 121 | + } else { |
| 122 | + temp[k++] = nums[j++]; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + for (i = low; i <= high; ++i) { |
| 127 | + nums[i] = temp[i]; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private static void mergeSort(int[] nums, int low, int high, int[] temp) { |
| 132 | + if (low >= high) { |
| 133 | + return; |
| 134 | + } |
| 135 | + int mid = low + ((high - low) >> 1); |
| 136 | + mergeSort(nums, low, mid, temp); |
| 137 | + mergeSort(nums, mid + 1, high, temp); |
| 138 | + merge(nums, low, mid, high, temp); |
| 139 | + } |
| 140 | + |
| 141 | + private static void mergeSort(int[] nums) { |
| 142 | + int n = nums.length; |
| 143 | + int[] temp = new int[n]; |
| 144 | + mergeSort(nums, 0, n - 1, temp); |
| 145 | + } |
| 146 | + |
| 147 | + public static void main(String[] args) { |
| 148 | + int[] nums = {1, 2, 7, 4, 5, 3}; |
| 149 | + mergeSort(nums); |
| 150 | + System.out.println(Arrays.toString(nums)); |
| 151 | + } |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +### 算法分析 |
| 156 | + |
| 157 | +空间复杂度 O(n),时间复杂度 O(nlogn)。 |
| 158 | + |
| 159 | +对于规模为 n 的问题,一共要进行 log(n) 次的切分,每一层的合并复杂度都是 O(n),所以整体时间复杂度为 O(nlogn)。 |
| 160 | + |
| 161 | +由于合并 n 个元素需要分配一个大小为 n 的额外数组,所以空间复杂度为 O(n)。 |
| 162 | + |
| 163 | +这是一种稳定的排序算法。 |
| 164 | + |
| 165 | +## 快速排序 |
| 166 | + |
| 167 | +快速排序也采用了分治的思想:把原始的数组筛选成较小和较大的两个子数组,然后递归地排序两个子数组。 |
0 commit comments