查找两个有序数组中的第K个元素

int FindKth(int a[], int b[], int k, int astart, int aend, int bstart, int bend)
{
int aLen = aend - astart + ;
int bLen = bend - bstart + ; if (aLen == )
{
return b[bstart + k];
} if (bLen == )
{
return a[astart + k];
} if (k == )
{
return a[astart] > b[bstart] ? b[bstart] : a[astart] ;
} int amid = aLen * k / (aLen + bLen); //按比例,算出分界点
int bmid = k - amid - ; amid += astart;
bmid += bstart; if (a[amid] > b[bmid])
{
k -= (bmid - bstart + );
aend = amid;
bstart = bmid + ;
}
else
{
k -= (amid - astart + );
bend = bmid;
astart = amid + ;
}
return FindKth(a, b, k, astart, aend, bstart, bend);
} int main(int argc, char* argv[])
{
int a[] = {, , , , , , };
int b[] = {, , , }; printf("\nfind 0th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 1th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 2th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 3th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 4th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 5th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 6th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 7th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 8th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 9th it=%d\n", FindKth(a, b, , , , , ));
printf("\nfind 10th it=%d\n", FindKth(a, b, , , , , )); getchar();
return ;
}

查找两个有序数组中的第K个元素(find kth smallest element in 2 sorted arrays)的更多相关文章

  1. Coursera Algorithms week3 快速排序 练习测验: Selection in two sorted arrays(从两个有序数组中寻找第K大元素)

    题目原文 Selection in two sorted arrays. Given two sorted arrays a[] and b[], of sizes n1 and n2, respec ...

  2. [转载]寻找两个有序数组中的第K个数或者中位数

    http://blog.csdn.net/realxie/article/details/8078043 假设有长度分为为M和N的两个升序数组A和B,在A和B两个数组中查找第K大的数,即将A和B按升序 ...

  3. 选取两个有序数组中最大的K个值,降序存入另一个数组中

    原题: 假设有两个有序的整型数组int *a1, int *a2,长度分别为m和n.试用C语言写出一个函数选取两个数组中最大的K个值(K可能大于m+n)写到int *a3中,保持a3降序,并返回a3实 ...

  4. Leetcode 703题数据流中的第K大元素(Kth Largest Element in a Stream)Java语言求解

    题目链接 https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/ 题目内容 设计一个找到数据流中第K大元素的类(class) ...

  5. LeetCode 378. 有序矩阵中第K小的元素(Kth Smallest Element in a Sorted Matrix) 13

    378. 有序矩阵中第K小的元素 378. Kth Smallest Element in a Sorted Matrix 题目描述 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩 ...

  6. 两个有序数组中的中位数以及求第k个最小数的值

    解法参考 <[分步详解]两个有序数组中的中位数和Top K问题> https://blog.csdn.net/hk2291976/article/details/51107778 里面求中 ...

  7. [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  8. 如何寻找无序数组中的第K大元素?

    如何寻找无序数组中的第K大元素? 有这样一个算法题:有一个无序数组,要求找出数组中的第K大元素.比如给定的无序数组如下所示: 如果k=6,也就是要寻找第6大的元素,很显然,数组中第一大元素是24,第二 ...

  9. [LeetCode] 378. Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

随机推荐

  1. 32.把数组排成最小的数(python)

    题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. # -*- ...

  2. 如何开始使用 Akka

    如果你是第一次开始使用 Akka,我们推荐你先运行简单的 Hello World 项目.情况参考  Quickstart Guide 页面中的内容来下载和运行 Hello World 示例程序.上面链 ...

  3. Vue组件使用

    一.组件概念 有html模板,有css样式,有js逻辑的集合体 每一个组件都是一个vue实例 每个组件均具有自身的模板template,根组件的模板就是挂载点 每个组件模板只能拥有一个根标签 子组件的 ...

  4. jQuery_完成表单注册检验

    在校验表单的时候会很麻烦,但是jq可以用很简单的方法来检验,即使用validate. 原表单: 代码 <!DOCTYPE html> <html> <head> & ...

  5. python爬取智联招聘职位信息(多进程)

    测试了下,采用单进程爬取5000条数据大概需要22分钟,速度太慢了点.我们把脚本改进下,采用多进程. 首先获取所有要爬取的URL,在这里不建议使用集合,字典或列表的数据类型来保存这些URL,因为数据量 ...

  6. xwiki使用中的问题

    xwiki 内存限制 问题重现: xwiki启动后内存.cpu一直上涨,不回落,启动后服务访问速度越来越慢,最后无法访问 分析: xwiki在启动时会消耗大量内存和cpu,增加tomcat最大内存限制 ...

  7. Link Script 学习

    最后更新 2019-06-27 概述 当使用 C 或者 C++ 编写代码实现某种功能时,需要将源代码进行编译以及链接.链接是将一系列目标文件(.o)以及归档文件(.a)组合起来,重新定位各个文件数据并 ...

  8. perl基础-2

    函数参数 perl 函数参数为$$,$$$,$@ Perl 可以通过函数元型在编译期进行有限的参数类型检验. 如果你声明 sub mypush (+@)那么 mypush() 对参数的处理就同内置的 ...

  9. Atcoder ARC101 E 树dp

    https://arc101.contest.atcoder.jp/tasks/arc101_c 题解是也是dp,好像是容斥做的,但是看不懂,而且也好像没讲怎么变n^2,看了写大佬的代码,自己理解了一 ...

  10. Intelij IDEA创建SpringBoot项目 - 配置文件的解释

    springboot介绍 官网:spring.io Spring Boot is designed to get you up and running as quickly as possible, ...