最近在纸上写一个已排序数组的合并时,花了超过预期的时间。仔细想想,这种要放到毕业找工作那会两下就出来了,原因还在于工作后对基础没有重视,疏于练习。

说开一点,现在搜索引擎的发达确实给问题的解决带来了便利,但是久而久之,对很多东西的掌握其实并不深入。比如淘宝系的人经常分享一些linux内核IO优化相关的内容,咋看一下,原来是这样,觉得也不难嘛,其实不然,如果给一张白纸让你自己把流程画出来,讲解清楚,还有有难度的。这里问题的关键在于很多时候我们只是通过互联网的便利了解某个东西,实际上并不掌握它。

纸上得来终觉浅,绝知此事要躬行。古人早就把道理告诉了我们,只是知易行难而已。

现在IT界新语言、新概念层出不穷,我也很喜欢玩这些新的东西并乐在其中,不过基础始终很重要。

回到正题,以后尽量养成动手的习惯,不管东西大小,内容高深与否,都自己实际过一遍,在博客里记录下来。

问题:将两个已排序数组合并成一个排序数组

这里先不考虑大数据量的情况(在数据量很大时不知大家有什么好的思路或方法?),只做简单数组的处理。

简单代码如下:

说明:之所以把merge函数定义成返回数组长度,是因为后续会有重复数据合并功能的merge版本,考虑到接口一致性。

#include <stdio.h>
#include <stdlib.h>
#include <string.h> int merge(int* ar1, int len1, int* ar2, int len2, int** rtn)
/*++
DeScription:
This routine merge two sorted arrays into one sorted array,
the same values in different arrays will be keeped. Arguments:
ar1 - The first sorted array to be merged
len1 - The num of items in ar1
ar2 - The second sorted array to be merged
len2 - The num of items in ar2
rtn - The caller proviced pointer to get the result array,
memory allocated for rtn should be free by the caller. Return Value:
The num of items in the merge array
--*/
{
int i=0,j=0,k=0;
int m=0;
int* res = NULL; if (ar1 == NULL || ar2 == NULL || rtn == NULL) {
return 0;
} *rtn = (int *)malloc((len1+len2)*sizeof(int));
if(*rtn == NULL) {
return 0;
}
memset(*rtn, 0, (len1+len2)*sizeof(int));
res = (int*)*rtn; while(i<len1 && j<len2) {
if (ar1[i]<=ar2[j]) {
res[k++] = ar1[i++];
} else {
res[k++] = ar2[j++];
}
} while(i<len1) {
res[k++] = ar1[i++];
}
while(j<len2) {
res[k++] = ar2[j++];
} return len1+len2;
} int merge_test()
{
int a1[] = {0,1,2,5,8,19,34,43,52};
int a2[] = {1,4,5,12,17,33,42,51,53,65,76};
int len1 = sizeof(a1)/sizeof(int);
int len2 = sizeof(a2)/sizeof(int);
int i = 0, len = 0;
int* a3 = NULL;
int* ptr = NULL; len = merge(a1, len1, a2, len2, &a3);
if (a3 == NULL) {
printf("a3==NULL\n");
return 1;
} ptr = a3;
while(i<len) {
printf("a3[%3d]---->%8d\n", i++, *ptr++);
} if (a3 != NULL) {
free(a3);
} return 0;
} int main(int argc, char* argv[])
{
merge_test(); return 0;
}

  

两个已排序数组的合并-C语言的更多相关文章

  1. 两个已排序数组进行合并后的第K大的值--进军硅谷

    我看到此题时,首先想到一个一个比较遍历过去,这是最暴力的方法,后面我想到了已经排序,那么对每个数组进行二分,然后比较这两个值.此书第三种解法,挺不错,只对那个长度较小的数组进行二分查找,保证i+j-1 ...

  2. leetcode 题解:Merge Sorted Array(两个已排序数组归并)

    题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume ...

  3. LeetCode第[88]题(Java):Merge Sorted Array(合并已排序数组)

    题目:合并已排序数组 难度:Easy 题目内容: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as ...

  4. 剑指Offer15 合并两个已排序链表

    /************************************************************************* > File Name: 15_MergeT ...

  5. LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

  6. C++版 - 剑指offer面试题38:数字在已排序数组中出现的次数

    数字在已排序数组中出现的次数 提交网址: http://www.nowcoder.com/practice/70610bf967994b22bb1c26f9ae901fa2?tpId=13&t ...

  7. leetcode题解:Search for a Range (已排序数组范围查找)

    题目: Given a sorted array of integers, find the starting and ending position of a given target value. ...

  8. 求两个等长的已排序数组的中位数(《算法导论》P113习题9.3-8)

    [问题]设X[1...n]和Y[1...n]为两个数组,每个都包含n个已排序好的数.给出一个求数组X和Y中所有2n个元素的中位数的.O(lgn)时间的算法. [解析]O(lgn)的时间复杂度就是二分查 ...

  9. comm - 逐行比较两个已排序的文件

    总览 (SYNOPSIS) ../src/comm [OPTION]... LEFT_FILE RIGHT_FILE 描述 (DESCRIPTION) 逐行比较 已排序的 文件 LEFT_FILE 和 ...

随机推荐

  1. JQ的双向数据绑定

    把渲染页面封装在function里面 在修改后直接调用,但是如果有alert();一定要注意function的摆放位置,正常应该是在alert前面,应为如果在后面会影响重新渲染的效果

  2. iOS多线程GCD的简单使用

    在iOS开发中,苹果提供了三种多线程技术,分别是: (1)NSThread (2)NSOperation (3)GCD 简单介绍一下GCD的使用. GCD全称 Grand Central Dispat ...

  3. ios::sync_with_stdio(false);

    取消cin与stdin的同步,加快输入速度

  4. Divide the Sequence

    Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission( ...

  5. E20180421-hm

    ambiguous  adj. 模棱两可; 含糊的,不明确的; 引起歧义的; 有两种或多种意思的; simple  adj. 简单的; 单纯的; 易受骗的; 天真的; simplify  vt. 简化 ...

  6. poj3249【拓扑排序】

    //题意:   给出一个有向无环图,每个顶点都有一个权值. //         求一条从入度为0的顶点到出度为0的顶点的一条路径, //         路径上所有顶点权值和最大. //我觉得只要明 ...

  7. 如何阻止浏览器的默认事件,你是否也遇到过无法阻止Google默认事件的情况( 原生JS )

    如题,话不多话,我们先看怎么解决 根据不同的绑定事件的方法,我们有不同的阻止默认事件的方法 如果你不知到如何绑定事件,请查看我的上一篇文章 关于浏览器滚动的兼容性问题以及事件绑定 1.句柄绑定 只需要 ...

  8. 6.while循环正向反向打印一句话

    message = "伤情最是晚凉天,憔悴厮人不堪言."' count=0 num=-1 while count<len(message): print(message[co ...

  9. 解决wubi安装ubuntu时要下载系统映像文件问题

    转载:一个人的旅行的博客(http://www.cnblogs.com/rollenholt/articles/2607433.html) 下面我介绍解决wubi安装ubuntu时要去官网下载系统映像 ...

  10. 安全性测试入门 (三):CSRF 跨站请求伪造攻击和防御

    本篇继续对于安全性测试话题,结合DVWA进行研习. CSRF(Cross-site request forgery):跨站请求伪造 1. 跨站请求伪造攻击 CSRF则通过伪装成受信任用户的请求来利用受 ...