Search the kth number from Sorted List---some thought
refer: 程序员内功修炼1.0
Problem1: given two sorted arrays with the same length, find the upper median of those two arrays
key words: sorted. which means we may can use binary search.
when there is only one sorted array and we want to find the median of that, we need to alter on the length of array is odd or even.because if it is odd, then we just need to exact it’s middle as the median, if not, we need to take average of two element.
now, the situation gets a little more tricky: we know the final median of those two array(with the same length!) must comes from average. how can we find those two elements?


so the basic idea is, when arr1[mid1]<arr2[mid2], the target must in the right part of the arr1 and the left part of arr2. and when arr1[mid1]>arr2[mid2], the target we want to find lies in right part of arr1 and right part of arr2. if they are equal, we can either return arr1[mid1] or arr2[mid2]
OR we can use iterative:

Problem2: given two sorted arrays, find the kth smallest number. and now this time, those two sorted array are not the same length.


Problem3: given two sorted array, find the median of all elements. and now this time, two arrays are may not the same length and we have to get the median.



浙公网安备 33010602011771号