leetcode练习:912. 排序数组

    1. const arr = Array(11).fill().map(() => ~~(Math.random() * 100));
    2. console.log('bubbleSort(arr): ', ...bubbleSort(arr.slice()));
    3. console.log('selectionSort(arr): ', ...selectionSort(arr.slice()));
    4. console.log('insertionSort(arr): ', ...insertionSort(arr.slice()));
    5. console.log('shellSort(arr): ', ...shellSort(arr.slice()));
    6. console.log('countingSort(arr): ', ...countingSort(arr.slice()));
    7. console.log('mergeSort1(arr): ', ...mergeSort1(arr.slice()));
    8. console.log('quickSort1(arr): ', ...quickSort1(arr.slice()));
    9. console.log('heapSort(arr): ', ...heapSort(arr.slice()));
    10. console.log('bucketSort(arr): ', ...bucketSort(arr.slice()));
    11. console.log('radixSort(arr): ', ...radixSort(arr.slice()));
    12. function bubbleSort(arr) {
    13. const len = arr.length;
    14. for (let i = 0; i < len; i++) {
    15. for (let j = 0; j < len - i - 1; j++) {
    16. if (arr[j] > arr[j + 1]) {
    17. [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
    18. }
    19. }
    20. }
    21. return arr;
    22. }
    23. function selectionSort(arr) {
    24. const len = arr.length;
    25. for (let i = 0; i < len; i++) {
    26. let temp = i;
    27. for (let j = i + 1; j < len; j++) {
    28. if (arr[temp] > arr[j]) {
    29. temp = j;
    30. }
    31. }
    32. [arr[temp], arr[i]] = [arr[i], arr[temp]];
    33. }
    34. return arr;
    35. }
    36. function insertionSort(arr) {
    37. const len = arr.length;
    38. for (let i = 1; i < len; i++) {
    39. const current = arr[i];
    40. let preIndex = i - 1;
    41. while (preIndex >= 0 && arr[preIndex] > current) {
    42. arr[preIndex + 1] = arr[preIndex];
    43. preIndex--;
    44. }
    45. arr[preIndex + 1] = current;
    46. }
    47. return arr;
    48. }
    49. function shellSort(arr) {
    50. const len = arr.length;
    51. let increment = ~~(len / 2);
    52. while (increment > 0) {
    53. for (let i = increment; i < len; i++) {
    54. const current = arr[i];
    55. let preIndex = i - increment;
    56. while (preIndex >= 0 && arr[preIndex] > current) {
    57. arr[preIndex + increment] = arr[preIndex];
    58. preIndex -= increment;
    59. }
    60. arr[preIndex + increment] = current;
    61. }
    62. increment = ~~(increment / 2);
    63. }
    64. return arr;
    65. }
    66. function countingSort(arr) {
    67. const maxValue = Math.max(...arr);
    68. const countArr = Array(maxValue + 1).fill(0);
    69. arr.forEach(element => {
    70. countArr[element]++;
    71. });
    72. let sortedIndex = 0;
    73. countArr.forEach((count, index) => {
    74. while (count--) {
    75. arr[sortedIndex++] = index;
    76. }
    77. });
    78. return arr;
    79. }
    80. function mergeSort1(arr) {
    81. if (arr.length < 2) return arr;
    82. const mid = ~~(arr.length / 2);
    83. return merge(mergeSort1(arr.slice(0, mid)), mergeSort1(arr.slice(mid)))
    84. }
    85. function merge(left, right) {
    86. let i = 0, j = 0;
    87. const newArr = [];
    88. while (i < left.length && j < right.length) {
    89. if (left[i] <= right[j]) {
    90. newArr.push(left[i++]);
    91. } else {
    92. newArr.push(right[j++]);
    93. }
    94. }
    95. while (i < left.length) {
    96. newArr.push(left[i++]);
    97. }
    98. while (j < right.length) {
    99. newArr.push(right[j++]);
    100. }
    101. return newArr;
    102. };
    103. function quickSort1(arr, left = 0, right = arr.length - 1) {
    104. if (left < right) {
    105. const partitionIndex = partition1(arr, left, right);
    106. quickSort1(arr, left, partitionIndex - 1);
    107. quickSort1(arr, partitionIndex + 1, right);
    108. }
    109. return arr;
    110. }
    111. function partition1(arr, left, right) {
    112. const current = arr[left];
    113. while (left < right) {
    114. while (left < right && arr[right] >= current) {
    115. right--;
    116. }
    117. arr[left] = arr[right];
    118. while (left < right && arr[left] < current) {
    119. left++;
    120. }
    121. arr[right] = arr[left];
    122. }
    123. arr[left] = current;
    124. return left;
    125. }
    126. function quickSort2(arr, left = 0, right = arr.length - 1) {
    127. if (left < right) {
    128. const partitionIndex = quickSort2(arr, left, right);
    129. quickSort2(arr, left, partitionIndex - 1);
    130. quickSort2(arr, partitionIndex + 1, right);
    131. }
    132. return arr;
    133. }
    134. function partition2(arr, left, right) {
    135. const pivot = arr[left];
    136. let index = left + 1;
    137. for(let i = index; i <= right; i++) {
    138. if(arr[i] < pivot) {
    139. [ arr[i], arr[index] ] = [ arr[index], arr[i] ];
    140. index++;
    141. }
    142. }
    143. [ arr[left], arr[index - 1] ] = [ arr[index - 1], arr[left] ];
    144. return index - 1;
    145. }
    146. function heapSort(arr) {
    147. const len = arr.length;
    148. for (let i = Math.floor(len / 2); i >= 0; i--) {
    149. sift(arr, i, len - 1);
    150. }
    151. for (let i = len - 1; i > 0; i--) {
    152. [arr[0], arr[i]] = [arr[i], arr[0]];
    153. sift(arr, 0, i - 1);
    154. }
    155. return arr;
    156. }
    157. function sift(arr, low, high) {
    158. const current = arr[low];
    159. let i = low, j = 2 * i + 1;
    160. while (j <= high) {
    161. if (j < high && arr[j] < arr[j + 1]) {
    162. j = j + 1;
    163. }
    164. if (current < arr[j]) {
    165. arr[i] = arr[j];
    166. i = j;
    167. j = 2 * i;
    168. } else break;
    169. }
    170. arr[i] = current;
    171. }
    172. function bucketSort(arr) {
    173. const len = arr.length;
    174. let maxValue = arr[0], minValue = arr[0];
    175. for (const num of arr) {
    176. if (num > maxValue) maxValue = num;
    177. if (num < minValue) minValue = num;
    178. }
    179. const bucketSize = 5;
    180. const bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1;
    181. const bucket = Array(bucketCount).fill().map(() => []);
    182. for (let i = 0; i < len; i++) {
    183. bucket[Math.floor((arr[i] - minValue) / bucketCount)].push(arr[i]);
    184. }
    185. const newArr = [];
    186. for (let i = 0; i < bucketCount; i++) {
    187. if (bucket[i].length) {
    188. newArr.push(...insertionSort(bucket[i]));
    189. }
    190. }
    191. return newArr;
    192. }
    193. function radixSort(arr, radixBase = 10) {
    194. let maxValue = arr[0], minValue = arr[0];
    195. for (const num of arr) {
    196. if (num > maxValue) maxValue = num;
    197. if (num < minValue) minValue = num;
    198. }
    199. let currentDigit = 1;
    200. while ((maxValue - minValue) / currentDigit >= 1) {
    201. arr = countingSortForRadix(arr, radixBase, currentDigit, minValue);
    202. currentDigit *= radixBase;
    203. }
    204. return arr;
    205. }
    206. function countingSortForRadix(arr, radixBase, currentDigit, minValue) {
    207. const len = arr.length;
    208. for (const num of arr) {
    209. const bucketIndex = Math.floor((num - minValue) / currentDigit) % radixBase;
    210. bucket[bucketIndex]++;
    211. }
    212. for (let i = 1; i < len; i++) {
    213. bucket[i] += bucket[i - 1];
    214. }
    215. const res = [];
    216. for (let i = len - 1; i >= 0; i--) {
    217. const bucketIndex = Math.floor((arr[i] - minValue) / currentDigit) % radixBase;
    218. res[--bucket[bucketIndex]] = arr[i];
    219. }
    220. return res;
    221. }