合并数组并排序

有两个递增的数组, 要求不使用sort方法, 将其合并为一个同样是不递减的数组; 参数都是一维数字类型的数组

arr1 = [1, 3, 5, 6];
arr2 = [2, 4, 6];

function merge(arr1, arr2) {
  const array = [...arr1, ...arr2];
  return array.reduce((sorted, el) => {
    let index = 0;
    while (index < array.length && el >= array[index]) index++;
    sorted.splice(index, 0, el);
    return sorted;
  }, []);
}
console.log(merge(arr1, arr2))
posted @   MerLin97  阅读(87)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示