You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ES6中对两个数组实现插入排序以生成降序合并结果

Insertion Sort for Merging Two Arrays (Descending Order, 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 of var for better scope control.
  • for...of loops: 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 09:07:36