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. 关于bootstrap--表单(水平表单)

    在Bootstrap框架中要实现水平表单效果,必须满足以下两个条件:1.在<form>元素是使用类名“form-horizontal”.2.配合Bootstrap框架的网格系统.(网格布局 ...

  2. eclipse中使用git提交代码到github

    这里假设你已经拥有了github账号,建好了带提交的eclipse工程,进行了本地git的提交.本文只介绍在eclipse通过git插件将代码push到github 1.登录github新建repos ...

  3. testng 提供参数

    获取页面元素属性,并把属性作为参数传递个测试方法,两桶不同的写法 1. @DataProvider public Iterator<Object[]> dp() { mySleep(500 ...

  4. python高级编程:有用的设计模式2

    # -*- coding: utf-8 -*- __author__ = 'Administrator' #python高级编程:有用的设计模式 #代理 """ 代理对一 ...

  5. web前端之 DOM

    文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是,DOM把 ...

  6. 使用openCV的静态库编译

    转载请注明出处: http://www.cnblogs.com/sysuzyq/p/6183568.html By 少侠阿朱 摘要: 本文主要讲述如何使用opencv静态库进行编译,生成脱离openc ...

  7. 验证docker的Redis镜像也存在未授权访问漏洞

    看到了这篇老外的博客:Over 30% of Official Images in Docker Hub Contain High Priority Security Vulnerabilities于 ...

  8. iOS中AutoLayer自动布局流程及相关方法【转】

    转自:http://my.oschina.net/w11h22j33/blog/208574 关于UIView的Layer,IOS提供了三个方法: 1.layoutSubviews 在iOS5.1和之 ...

  9. FineUI布局应用(二)

    一.FineUI页面布局分为 1.Fit布局 <f:Panel ID="Panel1" Title="布局Fit(Layout=Fit)" runat=& ...

  10. .net文件下载方法汇总

    转载自:http://blog.sina.com.cn/s/blog_680942070101ahsq.html //TransmitFile实现下载 protected void Button1_C ...