求两个等长已排序数组的中位数(算法导论习题9.3-8)

代码如下:

<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>


执行结果如下:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值