give two sorted array, find the k-th smallest element of union of A and B
Give you two sorted array, find the k-th smallest elements of union of A and B, you can assume that there are no duplicate elements. the size of A is m, and size of B is n, both them are in acsending order.At first, we can use brute-force, malloc a new array of size m+n, and merge A and B into this new array, then we can get the k-th smallest element, or k-th smallest elements. time complexity is O(m+n), and the space complexity is O(m+n);
A better way, using two pointers: i, and j, i proints head of A, j points to head of B, at the same time make a new malloc of array of size k. then we compare A[i] and B[j], we get the minimum of them and increase pointer of the minimum one in order to get next index of the array its belonged to. time complexity is O(k), and space complexity is O(k). here is the codes:
[cpp]
#include<iostream>
#include<cassert>
using namespace std;
int *get_k_th_minimum(int *a, int len_a, int *b, int len_b, int k) {
int p = 0;
int i = 0;
int j = 0;
int *c = (int*)malloc(sizeof(int) * k);
if(a == NULL || b == NULL || k > (len_a + len_b)) {
return NULL;
}
while(p < k && i < len_a && j < len_b) {
if(a[i] < b[j]) {
c[p++] = a[i];
i++;
} else {
c[p++] = b[j];
j++;
}
}
return c;
}
void main() {
int a[] = {1, 3, 5, 7, 9};
int b[] = {2, 4, 6, 8, 10};
int len_a = sizeof(a) / sizeof(int);
int len_b = sizeof(b) / sizeof(int);
int k = 5;
int *p = get_k_th_minimum(a, len_a, b, len_b, k);
int i = 0;
if(p) {
for(i = 0; i < k; i++) {
cout << p[i] << " ";
}
cout << endl;
}
getchar();
}
Then a more better way, it takes O(lgm + lgn) time to get the k-th minimum elements, not all the number of k elements. then we will take O(k) time to get these elements from A and B. And space complexity is O(1).
We try to approach this tricky problem by comparing middle elements of A and B, which we identify as Ai and Bj. If Ai is between Bj-1 and Bj, it shows that Ai is the i+j+1(index is from zero) smallest element, including Bj-1 there are j elements in B smaller than Ai, and there are i elements smaller than Ai. so Ai is the i+j+1 smallest element in union of A and B. Therefore,if we choose i and j such that i + j + 1 = k. then we find the k-th smallest element. This is an important invariant that we must maintain for the correctness of this algorithm.
summerizing above is : Maintaining the invariant i + j = k - 1 if Bj-1 < Ai < Bj, then Ai must be the k-th smallest . if Ai-1 < Bj < Ai, then Bj must be the k-th smallest.
If one of the conditions above is satisfied, then we get k-th smallest element. If both are not, we have to consider what we should do next? First, if we assume this condition: Bj-1 < Ai < Bj is not satisfied, and we can get two reasons of this failure: one is Ai > Bj, and the other is Ai < Bj-1, then we also assume that this condition: Ai-1 < Bj < Ai is also not satisfied. Then we can clearly get that is: Ai < Bj-1 makes Ai or Bj is not the k-th smallest element.
We make a summerization than when Ai < Bj, then Ai < Bj-1, on the other hand, if Bj < Ai, then Bj < Ai-1.
Using the above relationship, we can get Ai and its lower portion could never be the k-th smallest element. Let me make a explanation, if we assume that
Bj-2< Ai < Bj-1, then Ai will be the i+1+j-1 smallest elements,i.e (k-1)th, and this is the best situation and how about Ai < Bj-2, or Ai < Bj-3, and how about Ai's lower portion? so we discard Ai and its lower portion and why not Bj and its lower portion, think about that is there any possibility that Ai+1 < Bj-1 < Ai+2, if this can be true, then Bj-1 is the k-th smallest element. And to Bj's uper portion we also discard, why? because Ai < Bj < Bj+1, and there are total j+1 + i + 1 elements smaller than Bj+1. so we discard it.
On the other hand, the case for Ai > Bj is just the other way around. Easy.
Below is the code and I have inserted lots of assertion (highly recommended programming style by the way) to help you understand the code. Note that the below code is an example of tail recursion, so you could technically convert it to an iterative method in a straightforward manner. However, I would leave it as it is, since this is how I derive the solution and it seemed more natural to be expressed in a recursive manner.
Another side note is regarding the choices of i and j. The below code would subdivide both arrays using its array sizes as weights. The reason is it might be able to guess the k-th element quicker (as long as the A and B is not differed in an extreme way; ie, all elements in A are smaller than B). If you are wondering, yes, you could choosei to be A’s middle. In theory, you could choose any values for i and j as long as the invariant i+j = k-1 is satisfied.
here is the code:
[cpp]
#include<iostream>
#include<cassert>
using namespace std;
int flag = 0;
int get_k_th_minimum(int *a, int m, int *b, int n, int k) {
assert(m >= 0);
assert(n >= 0);
assert(k <= (m + n));
int i = (int)((double)m / (m + n) * (k + 1));
int j = (k - 1) - i;
assert(i >= 0);
assert(j >= 0);
assert(i <= m);
assert(j <= n);
int Ai_1 = ((i == 0) ? INT_MIN : a[i-1]);
int Bj_1 = ((j == 0) ? INT_MIN : b[j-1]);
int Ai = ((i == m) ? INT_MAX : a[i]);
int Bj = ((j == n) ? INT_MAX : b[j]);
if(Bj_1 < Ai && Ai < Bj) {
flag = 0;
return Ai;
} else if(Ai_1 < Bj && Bj < Ai) {
flag = 1;
return Bj;
补充:软件开发 , C++ ,
上一个:排序算法入门之冒泡排序优化
下一个:排序算法入门之简单选择排序
- 更多C/C++疑问解答:
- 关于c++的cout输出的问题。
- 在学校里学过C和C++,不过学的很一般,现在自学C#,会不会很难?
- 全国计算机二级C语言笔试题
- 已知某树有2个2度结点,3个3度结点,4个4度结点,问有几个叶子结点?
- c++数据结构内部排序问题,整数排序
- 2012九月计算机二级C语言全国题库,,急求急求
- 如果assert只有一个字符串作为参数,是什么意思呢?
- C语言中,哪些运算符具有左结合性,哪些具有右结合性,帮忙总结下,谢谢了!
- 为什么用结构体编写的程序输入是,0输不出来啊~~~
- 将IEEE—754的十六进制转化为十进制浮点类型,用C或C++都行,多谢各位大侠啊,非常感谢!
- 为什么这个程序求不出公式?
- 这个链表倒置的算法请大家分析下
- c语言函数库调用
- C语言unsigned int纠错
- C语言快排求解啊