摘要: Exercises 9.3-8Let X[1 .. n] and Y [1 .. n] be two arrays, each containing n numbers already in sorted order. Give an O(lg n)-time algorithm to find the median of all 2n elements in arrays X and Y.解: int findMedian( X[1..n], Y[1..n]) // X 和 Y均为n个以排序的数组在 X,Y中取中位数,分别为m1,m2 O(1)若m1=m2, return m1//不妨设m. 阅读全文
posted @ 2011-09-07 22:40 freewater 阅读(938) 评论(0) 推荐(0) 编辑
摘要: Describe an O(n)-time algorithm that, given a set S of n distinct numbers and a positive integer k ≤ n, determines the k numbers in S that are closest to the median of S.solution:算法思想:1.找到数组a中第n/2小的数median;2.对a中非median数进行|a[i] - median|,得到一个大小为n - 1的数组distance;3.寻找distance中第k小的数值;4.对distance进行一次遍历,找 阅读全文
posted @ 2011-09-07 22:36 freewater 阅读(308) 评论(0) 推荐(0) 编辑