算法 - 折半查找(C#)

7 篇文章 0 订阅
  • 递归实现:

[csharp]  view plain  copy
 print ?
  1. // --------------------------------------------------------------------------------------------------------------------  
  2. // <copyright company="Chimomo's Company" file="Program.cs">  
  3. // Respect the work.  
  4. // </copyright>  
  5. // <summary>  
  6. // The binary search (recursive).  
  7. // [折半查找的前提]:  
  8. // 1、待查找序列必须采用顺序存储结构。  
  9. // 2、待查找序列必须是按关键字大小有序排列。  
  10. // </summary>  
  11. // --------------------------------------------------------------------------------------------------------------------  
  12.   
  13. namespace CSharpLearning  
  14. {  
  15.     using System;  
  16.   
  17.     /// <summary>  
  18.     /// The program.  
  19.     /// </summary>  
  20.     internal class Program  
  21.     {  
  22.         /// <summary>  
  23.         /// Entry point into console application.  
  24.         /// </summary>  
  25.         public static void Main()  
  26.         {  
  27.             int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };  
  28.             Console.WriteLine(BinarySearch(a, 6, 0, 9));  
  29.         }  
  30.   
  31.         /// <summary>  
  32.         /// 在下界为low,上界为high的有序数组a中折半查找数据元素x(递归查找)。  
  33.         /// </summary>  
  34.         /// <param name="a">  
  35.         /// 待查找数组。  
  36.         /// </param>  
  37.         /// <param name="x">  
  38.         /// 目标元素。  
  39.         /// </param>  
  40.         /// <param name="low">  
  41.         /// 数组元素下标的下界。  
  42.         /// </param>  
  43.         /// <param name="high">  
  44.         /// 数组元素下标的上界。  
  45.         /// </param>  
  46.         /// <returns>  
  47.         /// 若查找到目标元素则返回该目标元素在数组中的下标;否则返回-1。  
  48.         /// </returns>  
  49.         private static int BinarySearch(int[] a, int x, int low, int high)  
  50.         {  
  51.             if (low > high)  
  52.             {  
  53.                 return -1;  
  54.             }  
  55.   
  56.             int mid = (low + high) / 2;  
  57.             if (x == a[mid])  
  58.             {  
  59.                 return mid;  
  60.             }  
  61.   
  62.             return x < a[mid] ? BinarySearch(a, x, low, mid - 1) : BinarySearch(a, x, mid + 1, high);  
  63.         }  
  64.     }  
  65. }  
  66.   
  67. // Output:  
  68. /* 
  69. 5 
  70. */  
时间复杂度:O(log2n)

  • 非递归实现:

[csharp]  view plain  copy
 print ?
  1. // --------------------------------------------------------------------------------------------------------------------  
  2. // <copyright company="Chimomo's Company" file="Program.cs">  
  3. // Respect the work.  
  4. // </copyright>  
  5. // <summary>  
  6. // The binary search (not recursive).  
  7. // [折半查找的前提]:  
  8. // 1、待查找序列必须采用顺序存储结构。  
  9. // 2、待查找序列必须是按关键字大小有序排列。  
  10. // </summary>  
  11. // --------------------------------------------------------------------------------------------------------------------  
  12.   
  13. namespace CSharpLearning  
  14. {  
  15.     using System;  
  16.   
  17.     /// <summary>  
  18.     /// The program.  
  19.     /// </summary>  
  20.     internal class Program  
  21.     {  
  22.         /// <summary>  
  23.         /// Entry point into console application.  
  24.         /// </summary>  
  25.         public static void Main()  
  26.         {  
  27.             int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };  
  28.             Console.WriteLine(BinarySearch(a, 6, 9));  
  29.         }  
  30.   
  31.         /// <summary>  
  32.         /// 在长度为n的有序数组a中查找值为key的元素(非递归查找)。  
  33.         /// </summary>  
  34.         /// <param name="a">  
  35.         /// 待查找数组。  
  36.         /// </param>  
  37.         /// <param name="key">  
  38.         /// 目标元素。  
  39.         /// </param>  
  40.         /// <param name="n">  
  41.         /// 数组长度。  
  42.         /// </param>  
  43.         /// <returns>  
  44.         /// 若查找到目标元素则返回该目标元素在数组中的下标;否则返回-1。  
  45.         /// </returns>  
  46.         private static int BinarySearch(int[] a, int key, int n)  
  47.         {  
  48.             int low = 0;  
  49.             int high = n - 1;  
  50.             while (low <= high)  
  51.             {  
  52.                 int mid = (low + high) / 2;  
  53.                 if (a[mid] == key)  
  54.                 {  
  55.                     return mid;  
  56.                 }  
  57.   
  58.                 if (a[mid] < key)  
  59.                 {  
  60.                     low = mid + 1;  
  61.                 }  
  62.                 else  
  63.                 {  
  64.                     high = mid - 1;  
  65.                 }  
  66.             }  
  67.   
  68.             return -1;  
  69.         }  
  70.     }  
  71. }  
  72.   
  73. // Output:  
  74. /* 
  75. 5 
  76. */  

时间复杂度:O(log2n)

转自:http://blog.csdn.net/troubleshooter/article/details/4621272

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值