怎样合并排序数组(How to merge 2 sorted arrays?)
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?)的更多相关文章
- lintcode:合并排序数组
题目: 合并排序数组 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A=[1,2,3,4],B=[2,4,5,6],返回 [1,2,2,3,4,4,5,6] 挑战 你能否优化你的算法,如果 ...
- 6. 合并排序数组 II
6. Merge Two Sorted Arrays Description Merge two given sorted integer array A and B into a new sorte ...
- lintcode:合并排序数组 II
题目: 合并排序数组 II 合并两个排序的整数数组A和B变成一个新的数组. 样例 给出A = [1, 2, 3, empty, empty] B = [4,5] 合并之后A将变成[1,2,3,4,5] ...
- Merge k Sorted Arrays【合并k个有序数组】【优先队列】
Given k sorted integer arrays, merge them into one sorted array. Example Given 3 sorted arrays: [ [1 ...
- LintCode之合并排序数组II
题目描述: 分析:题目的意思是把数组A和数组B合并到数组A中,且数组A有足够的空间容纳A和B的元素,合并后的数组依然是有序的. 我的代码: public class Solution { /* * @ ...
- 【LeetCode】【数组归并】Merge k Sorted Lists
描述 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)
21. 合并两个有序链表 21. Merge Two Sorted Lists 题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode ...
- [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 ...
- [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. ...
随机推荐
- javaweb项目的优化
简单地来看一个浏览器用户访问的流程: 浏览器->服务器->返回结果显示 这么简单地看,可能想得到的优化手段很少,常见的可能就是优化sql,加快数据库处理:加个缓存,加快返回:使用静态文件, ...
- Django之Cookie与Session
一.cookie 1.cookie使用 def cookie(request): print(request.COOKIES) # 获取所有的COOKIES obj = render(request, ...
- OC教程10-NSNumber具体
NSNumber简单介绍 NSNumber是数字的对象形式,由于在OC的数组和字典中仅仅同意存放对象,所以我们有时候须要转化 我们普通的类型是 123 那么 NSNumber类型的是 @123, ...
- .NET基础拾遗(6)特性
1 神马是特性?如何自定义一个特性? (1)特性是什么 特性是一个对象,可以加载到程序集及程序集的对象中,这些对象包括 程序集本身.模块.类.接口.结构.构造函数.方法.方法参数等,加载了特性 ...
- (转)WCF中调用WebService出错,大家帮忙看看,回答就有分
http://bbs.csdn.net/topics/390542345 在WCF项目里面添加了一个WebService引用,然后在我们调用这个WCF服务时,老出错,提示在 ServiceModel ...
- http断点续传原理
断点续传一是断点,一续传. 断点是在下载时,将下载文件分多片,同时进行多片一起下载,如果任务被暂停,暂停的位置就是断点. 续传就是未完成的下载再次开始时,会从上次的断点继续传送. 在下载(或上传)过程 ...
- 三、服务解析(Resolving Services)
当你完成组件注册,并将组件暴露为适当的服务后你就可以通过容器或者容器的子生命周期域来解析服务(After you have your components registered with approp ...
- vc远程调试启动进程(非attach)
被调试端设置同attach进程方式的远程调试 代码端,需要在[Project] [Properties] [Configuration Properties] [Debugging].将Debugge ...
- QWidget使用qss样式的background-image属性
最近在学习Qt使用QSS样式美化窗口部件的内容.发现在对QWidget应用background-image改变窗口背景图片时,QWidget的窗口背景并未生效.工程建立如下: 1.新建 Qt A ...
- Java中list<Object>集合去重实例
一:Java中list去重的方法很多,下面说一下其中一种方法:把list里的对象遍历一遍,用list.contain(),如果不存在就放入到另外一个list集合中: 二:实例 这里需要注意的是:使用c ...