算法导论Java实现-二分查找运用(习题2.3-7)


 
  
  1. package lhz.algorithm.chapter.two; 
  2.  
  3. /** 
  4.  * 《算法导论》,习题2.3-7  
  5.  * Describe a Θ(n lg n)-time algorithm that, given a set S of n 
  6.  * integers and another integer x, determines whether or not there exist two 
  7.  * elements in S whose sum is exactly x. 
  8.  * @author lihzh(苦逼coder) 
  9. * 本文地址:http://mushiqianmeng.blog.51cto.com/3970029/732739  
  10. */ 
  11. public class Excercise237 { 
  12.  
  13.     private static int[] input = new int[] { 21549867103 }; 
  14.     private static int sum = 11
  15.  
  16.     public static void main(String[] args) { 
  17.         // 先利用合并排序,将给定数组排好序 
  18.         mergeSort(input);// 复杂度:Θ(nlgn) 
  19.         boolean isExist = false
  20.         // 设置循环结束位,每次找到符合条件的元素后,改变循环结束条件 
  21.         // 否则会出现11 = 1 + 10,11 = 10 + 1的重复情况。 
  22.         int end = input.length; 
  23.         for (int i = 0; i < end; i++) {// 复杂度:Θ(n) 
  24.             int temp = sum - input[i]; 
  25.             // 利用二分查找,查询temp是否在数组中。 
  26.             Integer index = binarySearch(input, temp, 0, input.length - 1);// 复杂度:Θ(lgn) 
  27.             if (index != null && index != i) {// 不等于本身 
  28.                 System.out.println(sum + " = " + input[i] + " + " + temp); 
  29.                 end = index; 
  30.                 isExist = true
  31.             } 
  32.         } 
  33.         if (!isExist) { 
  34.             System.out.println("数组不存在两个数的和为:" + sum); 
  35.         } 
  36.         /* 
  37.          * 复杂度分析: 由注释可见,复杂度为:Θ(nlgn) 
  38.          */ 
  39.     } 
  40.  
  41.     /** 
  42.      * 合并排序,复杂度:Θ(nlgn) 
  43.      *  
  44.      * @param array 
  45.      * @return 
  46.      */ 
  47.     private static int[] mergeSort(int[] array) { 
  48.         // 如果数组的长度大于1,继续分解数组 
  49.         if (array.length > 1) { 
  50.             int leftLength = array.length / 2
  51.             int rightLength = array.length - leftLength; 
  52.             // 创建两个新的数组 
  53.             int[] left = new int[leftLength]; 
  54.             int[] right = new int[rightLength]; 
  55.             // 将array中的值分别对应复制到两个子数组中 
  56.             for (int i = 0; i < leftLength; i++) { 
  57.                 left[i] = array[i]; 
  58.             } 
  59.             for (int i = 0; i < rightLength; i++) { 
  60.                 right[i] = array[leftLength + i]; 
  61.             } 
  62.             // 递归利用合并排序,排序子数组 
  63.             left = mergeSort(left); 
  64.             right = mergeSort(right); 
  65.             // 设置初始索引 
  66.             int i = 0
  67.             int j = 0
  68.             for (int k = 0; k < array.length; k++) { 
  69.                 // 如果左边数据索引到达边界则取右边的值 
  70.                 if (i == leftLength && j < rightLength) { 
  71.                     array[k] = right[j]; 
  72.                     j++; 
  73.                     // 如果右边数组索引到达边界,取左数组的值 
  74.                 } else if (i < leftLength && j == rightLength) { 
  75.                     array[k] = left[i]; 
  76.                     i++; 
  77.                     // 如果均为到达则取,较小的值 
  78.                 } else if (i < leftLength && j < rightLength) { 
  79.                     if (left[i] > right[j]) { 
  80.                         array[k] = right[j]; 
  81.                         j++; 
  82.                     } else { 
  83.                         array[k] = left[i]; 
  84.                         i++; 
  85.                     } 
  86.                 } 
  87.             } 
  88.         } 
  89.         return array; 
  90.     } 
  91.  
  92.     /** 
  93.      * 二分查找,复杂度Θ(lg n) 
  94.      *  
  95.      * @param input 
  96.      * @param target 
  97.      * @param from 
  98.      * @param to 
  99.      * @return 
  100.      */ 
  101.     private static Integer binarySearch(int[] input, int target, int from, 
  102.             int to) { 
  103.         int range = to - from; 
  104.         // 如果范围大于0,即存在两个以上的元素,则继续拆分 
  105.         if (range > 0) { 
  106.             // 选定中间位 
  107.             int mid = (to + from) / 2
  108.             // 先判断两个二分的临界位 
  109.             if (input[mid] == target) { 
  110.                 return mid; 
  111.             } 
  112.             // 如果临界位不满足,则继续二分查找 
  113.             if (input[mid] > target) { 
  114.                 return binarySearch(input, target, from, mid - 1); 
  115.             } else { 
  116.                 return binarySearch(input, target, mid + 1, to); 
  117.             } 
  118.         } else { 
  119.             if (input[from] == target) { 
  120.                 return from; 
  121.             } else { 
  122.                 return null
  123.             } 
  124.         } 
  125.     } 

 


     本文转自mushiqianmeng 51CTO博客,原文链接:http://blog.51cto.com/mushiqianmeng/732739,如需转载请自行联系原作者




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值