题目原文

Selection in two sorted arrays. Given two sorted arrays a[] and b[], of sizes n1 and n2, respectively, design an algorithm to find the kth largest key. The order  of growth of the worst case running time of your algorithm should be logn, where n = n1 + n2.

Version 1 : n1 = n2 and k = n/2

Version 2: k = n/2

Version 3: no restrictions

分析:

这个题目是要求从两个有序数组中寻找第K大元素,直观解法就是把连个数组归并排序,这样时间复杂度就是O(n),但是题目只要求知道第K大元素,其余比K大的元素的排列顺序并不关心。

默认数组a和数组b都是从小到大排序的。不论第K大元素在a或b中,其右侧元素一定大于K,可考虑分别从a和b中排除右侧(k-1)/2个元素,剩下的就是在未排除区间寻找第(k-已排除元素)大元素。通过递归逐步缩小比较范围。具体算法见如下代码。

 package week3;

 import java.util.Arrays;
import edu.princeton.cs.algs4.StdRandom; public class KthInTwoSortedArrays {
/**
* 寻找第K大元素
* @param a 数组a
* @param alo a的查找区间下界
* @param ahi a的查找区间上界
* @param b 数组b
* @param blo b的查找区间下界
* @param bhi b的查找区间上界
* @param k 当前查找区间的第k大元素
* @return
*/
private static int find(int[] a, int alo, int ahi,int[] b, int blo, int bhi, int k){
if(alo > ahi) return b[bhi-k+1];
if(blo > bhi) return a[ahi-k+1];
if(k==1) return a[ahi] > b[bhi]? a[ahi]:b[bhi];
//直接用bhi-(k-1)/2或ahi-(k-1)/2进行查找可能会将第K个元素给漏掉
int bt = bhi-(k-1)/2 > blo ? bhi-(k-1)/2:blo;
int at = ahi-(k-1)/2 > alo ? ahi-(k-1)/2:alo;
if(a[at] >= b[bt])
return find(a,alo,at-1,b,blo,bhi,k-(ahi-at+1));
else
return find(a,alo,ahi,b,blo,bt-1,k-(bhi-bt+1));
} public static void main(String[] args){
int n = 10;
int n1 = StdRandom.uniform(n);
int n2 = n-n1;
int[] a = new int[n1];
int[] b = new int[n2];
for(int i=0;i<n1;i++){
a[i] = StdRandom.uniform(100);
}
for(int i=0;i<n2;i++){
b[i] = StdRandom.uniform(100);
}
Arrays.sort(a);
Arrays.sort(b);
System.out.println("a="+Arrays.toString(a));
System.out.println("b="+Arrays.toString(b));
int[] c = new int[n];
int i = 0;
int j = 0;
int l = 0;
while(l<n){
if(i>=n1) c[l++] = b[j++];
else if(j>=n2) c[l++] = a[i++];
else{
if(a[i] <= b[j])
c[l++] = a[i++];
else
c[l++] = b[j++];
} }
System.out.println("c="+Arrays.toString(c)); int k =StdRandom.uniform(1,n);
int largestK = find(a,0,n1-1,b,0,n2-1,k);
System.out.println("第"+k+"大元素是:"+largestK);
}
}

Coursera Algorithms week3 快速排序 练习测验: Selection in two sorted arrays(从两个有序数组中寻找第K大元素)的更多相关文章

  1. Coursera Algorithms week3 快速排序 练习测验: Nuts and bolts

    题目原文: Nuts and bolts. A disorganized carpenter has a mixed pile of n nuts and n bolts. The goal is t ...

  2. Coursera Algorithms week3 快速排序 练习测验: Decimal dominants(寻找出现次数大于n/10的元素)

    题目原文: Decimal dominants. Given an array with n keys, design an algorithm to find all values that occ ...

  3. Coursera Algorithms week3 归并排序 练习测验: Shuffling a linked list

    题目原文: Shuffling a linked list. Given a singly-linked list containing n items, rearrange the items un ...

  4. Coursera Algorithms week3 归并排序 练习测验: Counting inversions

    题目原文: An inversion in an array a[] is a pair of entries a[i] and a[j] such that i<j but a[i]>a ...

  5. Coursera Algorithms week3 归并排序 练习测验: Merging with smaller auxiliary array

    题目原文: Suppose that the subarray a[0] to a[n-1] is sorted and the subarray a[n] to a[2*n-1] is sorted ...

  6. 无序数组中用 快速排序的分治思想 寻找第k大元素

    #include <stdio.h> int *ga; int galen; void print_a(){ ; i < galen; i++){ printf("%d & ...

  7. 寻找第K大的数(快速排序的应用)

    有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数.给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在.测试样例:[1,3,5,2,2],5, ...

  8. 快速排序 && 寻找第K大(小)的数

    参考:https://minenet.me/2016/08/24/quickSort.html 快速排序 利用分治法可将快速排序的分为三步: 在数据集之中,选择一个元素作为"基准" ...

  9. 215. 数组中的第K个最大元素 + 快速排序 + 大根堆

    215. 数组中的第K个最大元素 LeetCode-215 另一道类似的第k大元素问题:https://www.cnblogs.com/GarrettWale/p/14386862.html 题目详情 ...

随机推荐

  1. wpf 界面加载 Command

    导入 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" <i:Interaction. ...

  2. poj - 3254 - Corn Fields (状态压缩)

    poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...

  3. 经典书籍---MySQL经典书籍下载

    以下是一些经典的MySQL书籍电子版,括号内为提取码,若需自取. 欢迎阅读纸质版,尊重作者版权 高性能MySQL_中文版 [ hre3 ] 高性能MySQL_英文版[ m2xj ] MySQL技术内幕 ...

  4. 【ssm】spring功能讲解

    概览 Spring5框架包含许多特性,负责管理项目中的所有对象,并被很好地组织在下图所示的模块中 核心容器:由spring-beans.spring-core.spring-context.sprin ...

  5. Linux—Ubuntu14.0.5配置JAVA环境

    1. 前往ORACLE官网下载最新版本的Java JDK:http://www.oracle.com/technetwork/java/javase/downloads/index.html,默认下载 ...

  6. idea搭建maven项目 【转发】

    为了创建maven项目可是花了我时间了,网上的教程跟我的实际情况不符合,尤其是facets .artifacts 那块.幸亏找到这篇文章没解决了我的问题,他的描述跟我的情况一模一样.这篇文章竟然来自百 ...

  7. python virtualenv 虚拟环境的应用

    为什么要使用python的虚拟环境呢?: 首先我们来说不实用虚拟环境的情况: 在Python应用程序开发的过程中,系统安装的Python3只有一个版本:3.7.所有第三方的包都会被pip3安装到   ...

  8. [bzoj3012][luogu3065][USACO12DEC][第一!First!] (trie+拓扑排序判环)

    题目描述 Bessie has been playing with strings again. She found that by changing the order of the alphabe ...

  9. const int * 和 int * const 傻傻分不清楚

    const int * a和int const *a一样,定义时不是必须初始化,指针可以指向其他变量,但是指向的变量的值不能修改. int * const定义时必须初始化,即必须指明指向哪个变量,定义 ...

  10. v-on(事件处理)

    1.监听事件  v-on:click="msg+=1" (msg是写在data里) 2.方法事件处理器 v-on:click = "jia" (jia是写在me ...