1111
1212#include < stdio.h>
1313
14+ // Classical binary search algorithm, but slightly different
15+ // if cannot find the key, return the position where can insert the key
1416int binarySearch (int A[], int low, int high, int key){
1517 while (low<=high){
1618 int mid = low + (high - low)/2 ;
@@ -24,17 +26,33 @@ int binarySearch(int A[], int low, int high, int key){
2426 return low;
2527}
2628
29+ // Notes:
30+ // I feel the following methods is quite complicated, it should have a better high clear and readable solution
2731double findMedianSortedArrayHelper (int A[], int m, int B[], int n, int lowA, int highA, int lowB, int highB) {
2832
33+ // Take the A[middle], search its position in B array
2934 int mid = lowA + (highA - lowA)/2 ;
3035 int pos = binarySearch (B, lowB, highB, A[mid]);
3136 int num = mid + pos;
37+ // If the A[middle] in B is B's middle place, then we can have the result
3238 if (num == (m+n)/2 ){
39+ // If two arrays total length is odd, just simply return the A[mid]
40+ // Why not return the B[pos] instead ?
41+ // suppose A={ 1,3,5 } B={ 2,4 }, then mid=1, pos=1
42+ // suppose A={ 3,5 } B={1,2,4}, then mid=0, pos=2
43+ // suppose A={ 1,3,4,5 } B={2}, then mid=1, pos=1
44+ // You can see, the `pos` is the place A[mid] can be inserted, so return A[mid]
3345 if ((m+n)%2 ==1 ){
3446 return A[mid];
3547 }
48+
49+ // If tow arrys total length is even, then we have to find the next one.
3650 int next;
3751
52+ // If both `mid` and `pos` are not the first postion.
53+ // Then, find max(A[mid-1], B[pos-1]).
54+ // Because the `mid` is the second middle number, we need to find the first middle number
55+ // Be careful about the edge case
3856 if (mid>0 && pos>0 ){
3957 next = A[mid-1 ]>B[pos-1 ] ? A[mid-1 ] : B[pos-1 ];
4058 }else if (pos>0 ){
@@ -45,14 +63,23 @@ double findMedianSortedArrayHelper(int A[], int m, int B[], int n, int lowA, int
4563
4664 return (A[mid] + next)/2.0 ;
4765 }
66+ // if A[mid] is in the left middle place of the whole two arrays
67+ //
68+ // A(len=16) B(len=10)
69+ // [................] [...........]
70+ // ^ ^
71+ // mid=7 pos=1
72+ //
73+ // move the `low` pointer to the "middle" position, do next iteration.
4874 if (num < (m+n)/2 ){
4975 lowA = mid + 1 ;
50- lowB = pos;
76+ lowB = pos;
5177 if ( highA - lowA > highB - lowB ) {
5278 return findMedianSortedArrayHelper (A, m, B, n, lowA, highA, lowB, highB);
5379 }
5480 return findMedianSortedArrayHelper (B, n, A, m, lowB, highB, lowA, highA);
5581 }
82+ // if A[mid] is in the right middle place of the whole two arrays
5683 if (num > (m+n)/2 ) {
5784 highA = mid - 1 ;
5885 highB = pos-1 ;
@@ -65,11 +92,17 @@ double findMedianSortedArrayHelper(int A[], int m, int B[], int n, int lowA, int
6592}
6693
6794double findMedianSortedArrays (int A[], int m, int B[], int n) {
95+
96+ // checking the edge cases
6897 if ( m==0 && n==0 ) return 0.0 ;
98+
99+ // if the length of array is odd, return the middle one
100+ // if the length of array is even, return the average of the middle two numbers
69101 if ( m==0 ) return n%2 ==1 ? B[n/2 ] : (B[n/2 -1 ] + B[n/2 ])/2.0 ;
70102 if ( n==0 ) return m%2 ==1 ? A[m/2 ] : (A[m/2 -1 ] + A[m/2 ])/2.0 ;
71103
72104
105+ // let the longer array be A, and the shoter array be B
73106 if ( m > n ){
74107 return findMedianSortedArrayHelper (A, m, B, n, 0 , m-1 , 0 , n-1 );
75108 }
0 commit comments