|
| 1 | +[TOC] |
| 2 | + |
1 | 3 | ### 排序算法
|
2 | 4 |
|
3 | 5 | | 算法 | 稳定 | 原地排序 | 时间复杂度 | 空间复杂度 | 备注 |
|
@@ -66,43 +68,71 @@ const insertSort = function (nums: number[]) {
|
66 | 68 |
|
67 | 69 | #### 希尔排序
|
68 | 70 |
|
69 |
| -```java |
70 |
| - |
| 71 | +```tsx |
| 72 | +const shellSort = (nums: number[]) => { |
| 73 | + const len = nums.length |
| 74 | + let h = len >> 1 |
| 75 | + while (h >= 1) { |
| 76 | + for (let i = h; i < len; i++) { |
| 77 | + for (let j = i; j >= h && nums[j] < nums[j - h]; j -= h) { |
| 78 | + [nums[j], nums[j - h]] = [nums[j - h], nums[j]] |
| 79 | + } |
| 80 | + } |
| 81 | + h >>= 1 |
| 82 | + } |
| 83 | +} |
71 | 84 | ```
|
72 | 85 |
|
73 | 86 | #### 归并排序
|
74 | 87 |
|
75 |
| -```java |
76 |
| - |
| 88 | +```tsx |
| 89 | +const mergeSort = (nums: number[], left: number, right: number) => { |
| 90 | + if (left === right) return [nums[left]] |
| 91 | + const mid = left + ((right - left) >> 1) |
| 92 | + const leftArray = mergeSort(nums, left, mid) |
| 93 | + const rightArray = mergeSort(nums, mid + 1, right) |
| 94 | + const newArray: number[] = new Array(leftArray.length + rightArray.length) |
| 95 | + let i = 0, j = 0, k = 0 |
| 96 | + while (i < leftArray.length && j < rightArray.length) { |
| 97 | + newArray[k++] = leftArray[i] < rightArray[j] ? leftArray[i++] : rightArray[j++] |
| 98 | + } |
| 99 | + while (i < leftArray.length) { |
| 100 | + newArray[k++] = leftArray[i++] |
| 101 | + } |
| 102 | + while (j < rightArray.length) { |
| 103 | + newArray[k++] = rightArray[j++] |
| 104 | + } |
| 105 | + return newArray |
| 106 | +} |
77 | 107 | ```
|
78 | 108 |
|
79 | 109 | #### 快速排序
|
80 | 110 |
|
81 |
| -```java |
| 111 | +```tsx |
82 | 112 | const quickSort = (nums: number[], start: number, end: number) => {
|
83 |
| - let left = start, |
84 |
| - right = end |
85 |
| - const pivot = start |
86 |
| - while (left < right) { |
87 |
| - while (left < right && nums[right] > nums[pivot]) { |
88 |
| - right-- |
89 |
| - } |
90 |
| - while (left < right && nums[left] < nums[pivot]) { |
91 |
| - left++ |
92 |
| - } |
93 |
| - if (nums[left] === nums[right] && left < right) { |
94 |
| - left++ |
95 |
| - } else { |
96 |
| - ;[nums[left], nums[right]] = [nums[right], nums[left]] |
97 |
| - } |
98 |
| - } |
99 |
| - if (start < left - 1) { |
100 |
| - quickSort(nums, start, left - 1) |
101 |
| - } |
102 |
| - if (right + 1 < end) { |
103 |
| - quickSort(nums, right + 1, end) |
104 |
| - } |
105 |
| - return nums |
| 113 | + let left = start, |
| 114 | + right = end |
| 115 | + const pivot = nums[start] |
| 116 | + while (left < right) { |
| 117 | + while (left < right && nums[right] > pivot) { |
| 118 | + right-- |
| 119 | + } |
| 120 | + while (left < right && nums[left] < pivot) { |
| 121 | + left++ |
| 122 | + } |
| 123 | + if (nums[left] === nums[right] && left < right) { |
| 124 | + left++ |
| 125 | + } else { |
| 126 | + ;[nums[left], nums[right]] = [nums[right], nums[left]] |
| 127 | + } |
| 128 | + } |
| 129 | + if (start < left - 1) { |
| 130 | + quickSort(nums, start, left - 1) |
| 131 | + } |
| 132 | + if (right + 1 < end) { |
| 133 | + quickSort(nums, right + 1, end) |
| 134 | + } |
| 135 | + return nums |
106 | 136 | }
|
107 | 137 | ```
|
108 | 138 |
|
|
0 commit comments