怎样合并排序数组(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. ...
随机推荐
- ibatis学习之道:ibatis的<[CDATA]>dynamic属性跟#$的应用
ibatis的<![CDATA]>,dynamic属性和#,$的应用 <![CDATA[ ]]>的正确使用 ibatis作为一种半自动化的OR Mapping工具,其灵活性 ...
- html5 手机APP计算高度问题
安卓手机型号比较多,会出现bottom:0 找不到底部的问题,所以需要计算手机可视区域高度,这样便于使用百分比适配 (function(window,undefined){ /** * js_heig ...
- python linecache标准库基础学习
#python标准库基础之:linecacge:高效读取文本文件#说明与作用"""可以从文件或者导入python模块获取文件,维护一个结果缓存,从而可以更高效地从相同文件 ...
- Pojo和JavaBean的区别(转载)
OJO(Plain Old Java Object)这个名字用来强调它是一个普通java对象,而不是一个特殊的对象. 2005年11月时,“POJO”主要用来指代那些没用遵从特定的Java对象模型,约 ...
- extern C的作用详解
extern "C"的主要作用就是为了能够正确实现C++代码调用其他C语言代码.加上extern "C"后,会指示编译器这部分代码按C语言的进行编译,而不是C+ ...
- 老生常谈的Javascript作用域问题
在前端学习中,作用域这个问题一直被广泛提起,什么是作用域,什么又是作用域链?在Javascript中,怎么去理解这些概念都是学好这门语言的关键,所以在学习前端开发的过程中,我需要也很有必要去学习和总结 ...
- Katana概述
OWIN owin是web services和framework组件之间的抽象.抽象包括两个核心要素: environment dictionary 这个数据结构存储处理HTTP请求必须的状态和相关的 ...
- UI基础视图----UILabel总结
UILabel是UIKit框架中非常常用的视图类,是UIView的子类,是UIWindow,UIImageView等的兄弟类,因为继承自UIView,所以继承了UIView中的属性和方法,大部分都可以 ...
- sql 创建临时表
declare @channelid varchar(100) set @channelid='''WH00026'',''WH00083''' declare @sql varchar(1000) ...
- C#通过生成ini文件,记住用户关闭程序之前的选择+忽略跨线程检查
1.在类的里面添加 //写配置文件 [DllImport("kernel32")] private static extern long WritePrivateProfileSt ...