在两个有序的数组中找第N个数,二分查找 O(lgm+lgn)级
2009-10-09 20:52
375 查看
Give a divide and conquer algorithm for the following problem:
(由于 A 是有序的,所以后一部分的所有元素大于前一部分)。A[x] = A的
后一部分的第一个元素。
2. 同理把 B 也平均分成前后两个部分,前部分有 y 个元素,后部分有 m-y 个元素。
B[y] = B的后一部分的第一个元素。
3. 由于两个数组都是被平均分割的,所以可以近似地认为 x = n/2, y = m/2。
这里不妨设 A[x] <= B[y](如果 A[x] > B[y] 处理过程和下面类似):
由于在 A 中,A[x] 前面有 x 个元素,在 B 中,B[y] 前面有 y 个元素,
并且又有 A[x] <= B[y],那么,合并以后,A[x]前面原来那些元素必然
也在B[y]前面,也就是说,B[y]前面至少会有 x + y 个元素,我们再规定
如果 A, B 中有相同元素,则合并后 A 中的元素排在 B 前面,那么归并
以后 A[x] 也会排在 B[y] 前面,于是乎合并之后 B[y] 至少有 x+y+1 个元素。
如果 k <= x+y+1,也就是说,合并后第 k 大的元素必然落在 B[y] 前面。
所以,原来在 B 数组中,第二部分(B[y]以及 B[y] 之后)那些元素都不可能
包含我们要找到内容(第 k 大元素),所以我们可以把他们排除掉。
这样就排除了 B 中一半的内容。
在 A 中,A[x] 及其后面有 n1-x 个元素,除去 A[x] 之后有 n-x-1 个元素,
B[y] 及其后面有 m-y 个元素。那么,由于 A[x] <= B[y],所以合并起来之后,
B[y] 后面那些元素必然也在 A[x] 后面,则合并后 A[x] 后面至少有
(n-x-1) + (m-y) = (n+m)-(x+y+1) 个元素。
如果 k > x+y+1,也就说,合并后第 k 大的元素必然落在 A[x] 后面。
所以,原来在 A 数组中,第一部分(A[x]之前)以及 A[x] 都不可能包含我们
要找的元素,所以我们可以把他们排除掉。这样就排除了 A 中一半的内容。
综上所诉,对于 k <= x+y+1 还是 k > x+y+1 我们都提出了解决的方案,并且每种方案
都能把 A 或者 B 的规模减小一半。减小了一半之后,我们将其作为一个新的问题
继续使用上面的算法处理,直到 A 或者 B 减小到足够小:
1. A没有了,这样只需要找出 B 中第 k 大的元素,也就是 B[k].
2. B没有了,同上结果就是 A[k].
代码如下:
[c-sharp] view plaincopy
/************************************************************************
* This is the practice1 of the Algorithms It solved the problem1 below:
*
* Give a divide and conquer algorithm for the following problem:
* you are given two sorted lists of size m and n, and are allowed
* unit time access to the ith element of each list. Give an O(lg m + lgn)
* time algorithm for computing the kth largest element in the union of the
* two lists. (For simplicity, you can assume that the elements of the
* two lists are distinct).
*
* The idea of the Algorithm in the help file idea.txt!!
*
* The Algorithm is designed by:Nanne
*
************************************************************************/
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int FindTheKth(int a[],int b[],int aLeft, int aRight, int bLeft, int bRight, int k);
int main(){
int sizeA,sizeB;
int Kth;
cout << "AĴС";
cin >> sizeA;
int *arrA = new int[sizeA];
cout << "" << sizeA << "" << endl;
for (int i = 0; i < sizeA; i++)
cin >> arrA[i];
cout << "BĴС";
cin >> sizeB;
int *arrB = new int[sizeB];
cout << "" << sizeB << "" << endl;
for (int i = 0; i < sizeB; i++)
cin >> arrB[i];
while(true){
cout << "õڼλ" << endl
<< "λҪ" << sizeA + sizeB << "(-1Ƴ):";
cin >> Kth;
if( Kth != -1){
int res = FindTheKth(arrA,arrB,0, sizeA - 1, 0, sizeB - 1, Kth);
if(res != -1)
cout << "" << Kth << "λǣ" << res << endl;
}
else
return 0;
}
}
int FindTheKth(int a[],int b[],int aLeft, int aRight, int bLeft, int bRight, int k) {
int aMid = (aLeft + aRight) / 2, bMid = (bLeft + bRight) / 2;
if (aLeft > aRight) return b[bLeft+k-1];
if (bLeft > bRight) return a[aLeft+k-1];
if (a[aMid] <= b[bMid]) {
if (k <= (aMid - aLeft) + (bMid - bLeft) + 1) {
return FindTheKth(a,b,aLeft, aRight, bLeft, bMid-1, k);
} else {
return FindTheKth(a,b,aMid+1, aRight, bLeft, bRight, k-(aMid-aLeft)-1);
}
} else {
if (k <= (aMid - aLeft) + (bMid - bLeft) + 1) {
return FindTheKth(a,b,aLeft, aMid-1, bLeft, bRight, k);
} else {
return FindTheKth(a,b,aLeft, aRight, bMid+1, bRight, k-(bMid-bLeft)-1);
}
}
return -1;
}
在两个有序的数组中找第N个数,O(lgm+lgn)级
分类: 算法2009-10-09 20:52 981人阅读 评论(3) 收藏 举报问题描述:
Give a divide and conquer algorithm for the following problem:
you are given two sorted lists of size m and n, and are allowed
unit time access to the ith element of each list. Give an O(lg m + lgn)
time algorithm for computing the kth largest element in the union of the two lists. (For simplicity, you can assume that the elements of the
two lists are distinct).
问题分析:
1. 把 A 平均分为前后两个部分,前部分有 x 个元素,后部分有 n-x 个元素(由于 A 是有序的,所以后一部分的所有元素大于前一部分)。A[x] = A的
后一部分的第一个元素。
2. 同理把 B 也平均分成前后两个部分,前部分有 y 个元素,后部分有 m-y 个元素。
B[y] = B的后一部分的第一个元素。
3. 由于两个数组都是被平均分割的,所以可以近似地认为 x = n/2, y = m/2。
这里不妨设 A[x] <= B[y](如果 A[x] > B[y] 处理过程和下面类似):
part1:
由于在 A 中,A[x] 前面有 x 个元素,在 B 中,B[y] 前面有 y 个元素,
并且又有 A[x] <= B[y],那么,合并以后,A[x]前面原来那些元素必然
也在B[y]前面,也就是说,B[y]前面至少会有 x + y 个元素,我们再规定
如果 A, B 中有相同元素,则合并后 A 中的元素排在 B 前面,那么归并
以后 A[x] 也会排在 B[y] 前面,于是乎合并之后 B[y] 至少有 x+y+1 个元素。
如果 k <= x+y+1,也就是说,合并后第 k 大的元素必然落在 B[y] 前面。
所以,原来在 B 数组中,第二部分(B[y]以及 B[y] 之后)那些元素都不可能
包含我们要找到内容(第 k 大元素),所以我们可以把他们排除掉。
这样就排除了 B 中一半的内容。
part2:
在 A 中,A[x] 及其后面有 n1-x 个元素,除去 A[x] 之后有 n-x-1 个元素,
B[y] 及其后面有 m-y 个元素。那么,由于 A[x] <= B[y],所以合并起来之后,
B[y] 后面那些元素必然也在 A[x] 后面,则合并后 A[x] 后面至少有
(n-x-1) + (m-y) = (n+m)-(x+y+1) 个元素。
如果 k > x+y+1,也就说,合并后第 k 大的元素必然落在 A[x] 后面。
所以,原来在 A 数组中,第一部分(A[x]之前)以及 A[x] 都不可能包含我们
要找的元素,所以我们可以把他们排除掉。这样就排除了 A 中一半的内容。
all:
综上所诉,对于 k <= x+y+1 还是 k > x+y+1 我们都提出了解决的方案,并且每种方案
都能把 A 或者 B 的规模减小一半。减小了一半之后,我们将其作为一个新的问题
继续使用上面的算法处理,直到 A 或者 B 减小到足够小:
1. A没有了,这样只需要找出 B 中第 k 大的元素,也就是 B[k].
2. B没有了,同上结果就是 A[k].
代码如下:
[c-sharp] view plaincopy
/************************************************************************
* This is the practice1 of the Algorithms It solved the problem1 below:
*
* Give a divide and conquer algorithm for the following problem:
* you are given two sorted lists of size m and n, and are allowed
* unit time access to the ith element of each list. Give an O(lg m + lgn)
* time algorithm for computing the kth largest element in the union of the
* two lists. (For simplicity, you can assume that the elements of the
* two lists are distinct).
*
* The idea of the Algorithm in the help file idea.txt!!
*
* The Algorithm is designed by:Nanne
*
************************************************************************/
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int FindTheKth(int a[],int b[],int aLeft, int aRight, int bLeft, int bRight, int k);
int main(){
int sizeA,sizeB;
int Kth;
cout << "AĴС";
cin >> sizeA;
int *arrA = new int[sizeA];
cout << "" << sizeA << "" << endl;
for (int i = 0; i < sizeA; i++)
cin >> arrA[i];
cout << "BĴС";
cin >> sizeB;
int *arrB = new int[sizeB];
cout << "" << sizeB << "" << endl;
for (int i = 0; i < sizeB; i++)
cin >> arrB[i];
while(true){
cout << "õڼλ" << endl
<< "λҪ" << sizeA + sizeB << "(-1Ƴ):";
cin >> Kth;
if( Kth != -1){
int res = FindTheKth(arrA,arrB,0, sizeA - 1, 0, sizeB - 1, Kth);
if(res != -1)
cout << "" << Kth << "λǣ" << res << endl;
}
else
return 0;
}
}
int FindTheKth(int a[],int b[],int aLeft, int aRight, int bLeft, int bRight, int k) {
int aMid = (aLeft + aRight) / 2, bMid = (bLeft + bRight) / 2;
if (aLeft > aRight) return b[bLeft+k-1];
if (bLeft > bRight) return a[aLeft+k-1];
if (a[aMid] <= b[bMid]) {
if (k <= (aMid - aLeft) + (bMid - bLeft) + 1) {
return FindTheKth(a,b,aLeft, aRight, bLeft, bMid-1, k);
} else {
return FindTheKth(a,b,aMid+1, aRight, bLeft, bRight, k-(aMid-aLeft)-1);
}
} else {
if (k <= (aMid - aLeft) + (bMid - bLeft) + 1) {
return FindTheKth(a,b,aLeft, aMid-1, bLeft, bRight, k);
} else {
return FindTheKth(a,b,aLeft, aRight, bMid+1, bRight, k-(bMid-bLeft)-1);
}
}
return -1;
}
相关文章推荐
- 在两个有序的数组中找第N个数,二分查找 O(lgm+lgn)级
- 在两个有序的数组中找第N个数,O(lgm+lgn)级
- 在两个有序的数组中找第N个数,O(lgm+lgn)级
- [LeetCode]Median of Two Sorted Arrays 二分查找两个有序数组的第k数(中位数)
- [LeetCode]Median of Two Sorted Arrays 二分查找两个有序数组的第k数(中位数)
- 程序员面试题目总结--数组(二)【二分查找、找出给定数字出现次数、两个有序整型数组交集、找出数组中唯一的重复元素、判断数组中的数值是否连续相邻】
- 有序数组的二分查找
- 二分搜索典型应用(一)从有序数组中查找某个值
- 有序数组的二分查找
- 对于一个有序数组,我们通常采用二分查找的方式来定位某一元素,请编写二分查找的算法,在数组中查找指定元素。 给定一个整数数组A及它的大小n,同时给定要查找的元素val,请返回它在数组中的位置(从0开始),若不存在该元素,返回-1。若该元素出现多次,请返回第一次出现的位置。
- 算法—8.有序数组中的二分查找
- (二分查找思想)从有序递增旋转数组45679123 中找到数字6的位置
- 【二分查找】找出两个长度相等的数组中所有值的 “上中位数”
- 二分求两个有序数组第k大的数
- 查找:链表顺序查找和有序数组二分查找
- 详解Java数据结构和算法(有序数组和二分查找)
- Median of Two Sorted Arrays---两个有序数组查找中位数
- 二分查找变体之循环有序数组找分界点
- hihocoder#1128之非有序数组二分查找
- 有序表查询之一:java实现整型数组二分查找