Question: We have 2 sorted arrays and we want to combine them into a single sorted array.

Input: arr1[] = 1, 4, 6, 8, 13, 25    ||     arr2[] = 2, 7, 10, 11, 19, 50
Output: 1, 2, 4, 6, 7, 8, 10, 11, 13, 19, 50

最简单的方法之一就是把两个数组复制到一个新的数组中,对这个新的数组进行排序。但这样就不能利用原来的两个数组已经排好序这个条件了。

我们需要一个不一样的方法。下面是可行方法之一:

  • 为两个数组初始化两个变量作为索引。
  • 假设i指向arr1[],j指向arr2[]。
  • 比较arr1[i],arr2[j],哪个小就将那个复制进新的数组,并增加相应的系数。
  • 重复上述步骤直到i和j都到达数组尾部。

相应的算法实现:

#include<stdio.h>

//a function to merge two arrays
//array1 is of size 'l'
//array2 is of size 'm'
//array3 is of size n=l+m
void merge(int arr1[], int arr2[], int arr3[], int l, int m, int n)
{
//3 counters to point at indexes of 3 arrays
int i,j,k;
i=j=k=0; //loop until the array 1 and array 2 are within bounds
while(i<l && j<m)
{
//find the smaller element among the two
//and increase the counter
if(arr1[i] < arr2[j])
{
arr3[k] = arr1[i]; //increment counter of 1st array
i++;
}
else
{
arr3[k] = arr2[j]; //increment counter of second array
j++;
} //increase the counter of the final array
k++;
} //now fill the remaining elements as it is since they are
//already sorted
while(i<l)
{
arr3[k] = arr1[i];
i++;
k++;
}
while(j<m)
{
arr3[k] = arr2[j];
j++;
k++;
}
} //driver program to test the above function
int main(void)
{
int arr1[5] = {1, 5, 9, 11, 15};
int arr2[5] = {2, 4, 13, 99, 100}; int arr3[10] = {0}; merge(arr1, arr2, arr3, 5, 5, 10); int i=0;
for(i=0;i<10;i++)
printf("%d ",arr3[i]); return 0;
}

怎样合并排序数组(How to merge 2 sorted arrays?)的更多相关文章

  1. lintcode:合并排序数组

    题目: 合并排序数组 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 挑战 你能否优化你的算法,如果 ...

  2. 6. 合并排序数组 II

    6. Merge Two Sorted Arrays Description Merge two given sorted integer array A and B into a new sorte ...

  3. lintcode:合并排序数组 II

    题目: 合并排序数组 II 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A = [1, 2, 3, empty, empty] B = [4,5] 合并之后A将变成[1,2,3,4,5] ...

  4. Merge k Sorted Arrays【合并k个有序数组】【优先队列】

    Given k sorted integer arrays, merge them into one sorted array. Example Given 3 sorted arrays: [ [1 ...

  5. LintCode之合并排序数组II

    题目描述: 分析:题目的意思是把数组A和数组B合并到数组A中,且数组A有足够的空间容纳A和B的元素,合并后的数组依然是有序的. 我的代码: public class Solution { /* * @ ...

  6. 【LeetCode】【数组归并】Merge k Sorted Lists

    描述 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  7. LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)

    21. 合并两个有序链表 21. Merge Two Sorted Lists 题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode ...

  8. [Swift]LeetCode21. 合并两个有序链表 | Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  9. [Swift]LeetCode81. 搜索旋转排序数组 II | Search in Rotated Sorted Array II

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

随机推荐

  1. poj 2049 Let it Bead(polya模板)

      Description Cannery Row percent of the target audience insists that the bracelets be unique. (Just ...

  2. 左侧高亮(js)

    //左导航高亮 (function(){ var leftnav = $('#leftnav');  //左侧ul var pi = __preset.curpathid; if(pi=='/acco ...

  3. 在sae配置django项目

    1:使用sae开发必须有sae帐号,自己注册吧: 2:在sae上新建立一个python项目: 3:建立完成之后新建一个版本,即版本1: 4:获取svn的地址在eclipse中check下来: 5:注意 ...

  4. javascript正則表達式 &quot;\b&quot;问题

    preface 昨晚在看<javascript权威指南>后.看见作者自己封装一个兼容全部浏览器的山寨HTML5新API classLIst类.自己想了想认为自己也要去玩一下.可是能力还是有 ...

  5. AAM(Active Appearance Model)算法介绍

    前面介绍ASM算法(http://blog.csdn.net/carson2005/article/details/8194317)的时候,笔者提到,ASM是基于统计形状模型的基础上进行的,而AAM则 ...

  6. Visual Studio 命中断点时 打印信息

    打印时间: 开始: {DateTime.Now.ToString()} 结束: {DateTime.Now.ToString()} 搜索 复制

  7. MySql查看表信息

    SELECT TABLE_NAME, TABLE_COMMENT -- 指定信息列 FROM `information_schema`.`tables` A WHERE A.`TABLE_SCHEMA ...

  8. js字母大小写转换

    function a(){ document.getElementById("test").value = document.getElementById("test&q ...

  9. EF中加载实体的方式

    EF中的查询执行时机:1. foreach进行枚举2. ToArray.ToList.ToDictionary3. Linq的一些操作,如First.Any4. DbSet上的Load操作.DbEnt ...

  10. oracle 数据库 if...elsif...语句

    CREATE OR REPLACE FUNCTION "UFN_GETIDS" (    OPEKIND   IN   VARCHAR2,-- 查询类型    PARAMS IN ...