题目原文

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. spring源码下载链接

    http://www.blogjava.net/zhyiwww/archive/2014/10/17/418809.html

  2. UVA-1368 DNA Consensus String(思路)

    题目: 链接 题意: 题目虽然比较长,但读完之后题目的思路还是比较容易想出来的. 给出m个长度为n的字符串(只包含‘A’.‘T’.‘G’.‘C’),我们的任务是得出一个字符串,要求这个字符串与给出的m ...

  3. 爬虫之Requests库

    官方文档:http://cn.python-requests.org/zh_CN/latest/ 一.引子 import requests resp = requests.get("http ...

  4. python3 的 zip

    准备放弃生命中这4个小时,然后翻开了python,人生苦短,音乐和python才味甘 1. zip  可以看到zip两个列表,返回一个元组的列表,但是它是个可迭代的对象,得用list才能调用显示: 2 ...

  5. LINUX-关机 (系统的关机、重启以及登出 )

     shutdown -h now 关闭系统(1) init 0 关闭系统(2) telinit 0 关闭系统(3) shutdown -h hours:minutes & 按预定时间关闭系统  ...

  6. Pygame游戏开发入门(1)-开发框架

    pygame库的安装 pip install pygame pygame最小开发框架 #Pygame Hello World Game import pygame,sys #引入pygame和sys( ...

  7. 后台工具screen

    之前在putty之类的远程命令行操作服务器的时候,遇到关闭软件,对应的操作就会关闭.很多时候,就是开着电脑,然后挂在那里,虽然不用电脑跑,但是也耗电...主要是putty这些软件有时候会伴随黑屏崩掉. ...

  8. PAT 1125 Chain the Ropes

    Given some segments of rope, you are supposed to chain them into one rope. Each time you may only fo ...

  9. 【Codeforces 1073D】Berland Fair

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 我们可以从左到右枚举一轮. 定义一个cost表示这一轮花费的钱数 如果cost+a[i]<=T那么就可以买它,并且买下它(模拟题目要求) ...

  10. lua 栈最后调用的函数,用于看调试信息

    lua_getinfo int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar); 返回一个指定的函数或函数调用的信息. 当用于取 ...