如何在ES6中对两个数组实现插入排序以生成降序合并结果
Hey there! I get that you want to ditch the concat().sort() approach for something that uses insertion sort specifically, and with ES6 syntax to keep it clean. Let's break this down step by step.
First, a quick recap: Insertion sort builds a sorted array one element at a time by taking each unsorted item and slotting it into its correct position in the already sorted portion. For merging your two arrays, we can either sort one first and insert the other's elements, or insert every element from both arrays into an empty result array directly—both work, and I'll show you both options.
Option 1: Sort one array first, then insert the other
This is efficient if one of your arrays is already partially sorted, but even if not, it's straightforward. Here's the code:
const arr1 = [90, 44, 64, 16, 24, 20, 64, 86, 20, 64, 56, 72, 16]; const arr2 = [21, 13, 9, 13, 15, 7, 17, 15, 9, 19, 7, 15, 9]; // Helper to sort a single array in descending order with insertion sort const insertionSortDesc = (arr) => { const sorted = [...arr]; // Use spread to avoid mutating original array for (let i = 1; i < sorted.length; i++) { const current = sorted[i]; let j = i - 1; // Shift elements smaller than current to the right (since we want descending) while (j >= 0 && sorted[j] < current) { sorted[j + 1] = sorted[j]; j--; } sorted[j + 1] = current; } return sorted; }; // Helper to insert elements from an unsorted array into a sorted one const insertIntoSorted = (sortedArr, unsortedArr) => { const result = [...sortedArr]; for (const num of unsortedArr) { let i = result.length - 1; while (i >= 0 && result[i] < num) { result[i + 1] = result[i]; i--; } result[i + 1] = num; } return result; }; // Put it all together const sortedArr1 = insertionSortDesc(arr1); const finalResult = insertIntoSorted(sortedArr1, arr2); console.log(finalResult); // Output matches your desired array: [90, 86, 72, 64, 64, 64, 56, 44, 24, 21, 20, 20, 19, 17, 16, 16, 15, 15, 15, 13, 13, 9, 9, 9, 7, 7]
Option 2: Insert all elements directly into an empty array
If you prefer a single function that handles both arrays without pre-sorting one, this works too. We just insert every element from both arrays into the result array one by one:
const arr1 = [90, 44, 64, 16, 24, 20, 64, 86, 20, 64, 56, 72, 16]; const arr2 = [21, 13, 9, 13, 15, 7, 17, 15, 9, 19, 7, 15, 9]; const mergeWithFullInsertionSort = (arrA, arrB) => { const result = []; // Insert all elements from first array for (const num of arrA) { let i = result.length - 1; while (i >= 0 && result[i] < num) { result[i + 1] = result[i]; i--; } result[i + 1] = num; } // Insert all elements from second array for (const num of arrB) { let i = result.length - 1; while (i >= 0 && result[i] < num) { result[i + 1] = result[i]; i--; } result[i + 1] = num; } return result; }; const finalResult = mergeWithFullInsertionSort(arr1, arr2);
ES6 Features Used Here:
- Spread operator (
...): Creates copies of arrays so we don't mutate the original inputs—super important for keeping your data clean. const/let: Block-scoped variables instead ofvarfor better scope control.for...ofloops: Clean, readable way to iterate through array elements without dealing with index counters directly.
Why This Is Better Than concat().sort() for Your Needs:
While the built-in sort is optimized (usually Timsort), your requirement is to use insertion sort specifically. Insertion sort shines when dealing with small or partially sorted datasets, and this implementation gives you full control over the sorting logic (in this case, descending order).
内容的提问来源于stack exchange,提问作者Martin

