Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Proof for the algorithm for m=n case. Let name middle element as "k"
- m1:=median (A, N) - O (1)
- m2:=median (B, N) - O (1)
- Proof:
- If m1 < m2, so m1 < k < m2 otherwise m2 < k < m1
- Without loss of generality –if m1 < k < m2, we know that the median of the two arrays must be
- Must be A[m1] < k <B [m2] so we can drop the irrelevant parts (A[0….m1] , B[m2……N]) by searching for new medians in each array.
- Proof by contradiction:
- Again without loss of generality…
- We will assume that k is found within ((A[0….m1] U B[m2……N]))
- Array A contains (((N+1)/2) -1) elements smaller than m1
- Array B contains maximum (((N+1)/2) -1) elements smaller than m2
- Unified set is - ((A[0….m1] U B[m2……N])) :
- (((N+1)/2) -1) + (((N+1)/2) -1) = N-1 elements before m1
- Contradicting the definition of median which is N= [((2N+1) / 2)]
- We can come to a conclusion that k cannot be a part of ((A[0….m1] U B[m2……N]))
- Therefore it must be found somewhere ((A[m1….n] U B[0….m2]))
- m1:=median (A+m1, N / 2) - O (1)
- m2:=median (B, N/ 2) - O (1)
- By reducing the size of the arrays by half each iteration we logarithmically decreasing the running time and thus improving efficiency – O (logN)
- And we repeat until size = 1 or 2
- Efficiency :
- T(N) = T(N/2) + 1
- T(1) = 1
- T(N) = T(N/2) + 1 = T(N/4) + 1 + 1 = .......
- T(N/(2^k)+k)
- N/(2^k) = 1
- k = logN so - > T(N) = logN + 1
- We can sum the efficiency of the algorithm to O (logN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement