代码如下:
<ol><li><span style="font-family: Arial, Helvetica, sans-serif;">package homework;</span></li><li>
</li><li><span style="font-family: Arial, Helvetica, sans-serif;">import static java.lang.Math.random;</span></li><li>
</li><li><span style="font-family: Arial, Helvetica, sans-serif;">import java.util.Arrays;</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;">/**</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> * 两个长度为n的有序数组的中位数。 复杂度O(lgn)</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> * </span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> */</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;">public class MiddleSelect {</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> public static final int N = 100;</span></li><li>
</li><li><span style="font-family: Arial, Helvetica, sans-serif;"> public static int middle(int[] x, int px, int rx, int[] y, int py, int ry) {</span></li><li>
</li><li><span style="font-family: Arial, Helvetica, sans-serif;"> // 无重叠,直接返回</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> if (x[N - 1] < y[0])</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> return x[N - 1];</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> if (y[N - 1] < x[0])</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> return y[N - 1];</span></li><li>
</li><li><span style="font-family: Arial, Helvetica, sans-serif;"> int mx = (px + rx) / 2;</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> int my = (py + ry) / 2;</span></li><li>
</li><li><span style="font-family: Arial, Helvetica, sans-serif;"> // x[i] 是中位数 <=> 有N-1个数比x[i]小 && 有N个数比x[i]大</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> // <=> y[N-i-2] <= x[i] <= y[N-i-1]</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> if (x[mx] > y[N - mx - 1])</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> rx = mx - 1;</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> else if (x[mx] < y[N - mx - 2])</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> px = mx + 1;</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> else</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> return x[mx];</span></li><li>
</li><li><span style="font-family: Arial, Helvetica, sans-serif;"> if (y[my] > x[N - my - 1])</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> ry = my - 1;</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> else if (y[my] < x[N - my - 2])</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> py = my + 1;</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> else</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> return y[my];</span></li><li>
</li><li><span style="font-family: Arial, Helvetica, sans-serif;"> return middle(x, px, rx, y, py, ry);</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> }</span></li><li>
</li><li><span style="font-family: Arial, Helvetica, sans-serif;"> // 测试</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> public static void main(String[] args) {</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> int a[] = new int[N];</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> int b[] = new int[N];</span></li><li>
</li><li><span style="font-family: Arial, Helvetica, sans-serif;"> int c[] = new int[2 * N];</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> for (int i = 0; i < 2 * N; i++) {</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> c[i] = (int) (10000 * random());</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> }</span></li><li>
</li><li><span style="font-family: Arial, Helvetica, sans-serif;"> for (int i = 0; i < N; i++) {</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> a[i] = c[i];</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> }</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> for (int i = N; i < 2 * N; i++) {</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> b[i - N] = c[i];</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> }</span></li><li>
</li><li><span style="font-family: Arial, Helvetica, sans-serif;"> Arrays.sort(a);</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> Arrays.sort(b);</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> Arrays.sort(c);</span></li><li>
</li><li><span style="font-family: Arial, Helvetica, sans-serif;"> for (int i = 0; i < a.length; i++) {</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> System.out.print(i + ": " + a[i] + " ");</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> }</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> System.out.println();</span></li><li>
</li><li><span style="font-family: Arial, Helvetica, sans-serif;"> for (int i = 0; i < b.length; i++) {</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> System.out.print(i + ": " + b[i] + " ");</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> }</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> System.out.println();</span></li><li>
</li><li><span style="font-family: Arial, Helvetica, sans-serif;"> for (int i = 0; i < c.length; i++) {</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> System.out.print(i + ": " + c[i] + " ");</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> }</span></li><li>
</li><li><span style="font-family: Arial, Helvetica, sans-serif;"> System.out.println();</span></li><li>
</li><li><span style="font-family: Arial, Helvetica, sans-serif;"> System.out.println(middle(a, 0, N - 1, b, 0, N - 1));</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;"> System.out.println(c[N - 2] + " " + c[N - 1] + " " + c[N]);</span></li><li>
</li><li><span style="font-family: Arial, Helvetica, sans-serif;"> }</span></li><li><span style="font-family: Arial, Helvetica, sans-serif;">}</span></li></ol>
执行结果如下: