Skip to content

Instantly share code, notes, and snippets.

@teabuff
Created February 11, 2016 01:46
Show Gist options
  • Save teabuff/5efc8420df0e57ff4145 to your computer and use it in GitHub Desktop.
Save teabuff/5efc8420df0e57ff4145 to your computer and use it in GitHub Desktop.
1. Median of Two Sorted Array in Same Size - Pseudocode
1) Calculate the medians m1 and m2 of the input arrays ar1[] and ar2[] respectively.
2) If m1 and m2 both are equal then we are done, and return m1 (or m2)
3) If m1 is greater than m2, then median is present in one of the below two subarrays.
a) From first element of ar1 to m1 (ar1[0...|_n/2_|])
b) From m2 to last element of ar2 (ar2[|_n/2_|...n-1])
4) If m2 is greater than m1, then median is present in one of the below two subarrays.
a) From m1 to last element of ar1 (ar1[|_n/2_|...n-1])
b) From first element of ar2 to m2 (ar2[0...|_n/2_|])
5) Repeat the above process until size of both the subarrays becomes 2.
6) If size of the two arrays is 2 then use below formula to get the median.
Median = (max(ar1[0], ar2[0]) + min(ar1[1], ar2[1]))/2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment