算法 - 折半查找(C#)

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               
  • 递归实现:

// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// The binary search (recursive).// [折半查找的前提]:// 1、待查找序列必须采用顺序存储结构。// 2、待查找序列必须是按关键字大小有序排列。// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    /// <summary>    /// The program.    /// </summary>    internal class Program    {        /// <summary>        /// Entry point into console application.        /// </summary>        public static void Main()        {            int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };            Console.WriteLine(BinarySearch(a, 6, 0, 9));        }        /// <summary>        /// 在下界为low,上界为high的有序数组a中折半查找数据元素x(递归查找)。        /// </summary>        /// <param name="a">        /// 待查找数组。        /// </param>        /// <param name="x">        /// 目标元素。        /// </param>        /// <param name="low">        /// 数组元素下标的下界。        /// </param>        /// <param name="high">        /// 数组元素下标的上界。        /// </param>        /// <returns>        /// 若查找到目标元素则返回该目标元素在数组中的下标;否则返回-1。        /// </returns>        private static int BinarySearch(int[] a, int x, int low, int high)        {            if (low > high)            {                return -1;            }            int mid = (low + high) / 2;            if (x == a[mid])            {                return mid;            }            return x < a[mid] ? BinarySearch(a, x, low, mid - 1) : BinarySearch(a, x, mid + 1, high);        }    }}// Output:/*5*/
时间复杂度:O(log2n)
  • 非递归实现:

// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// The binary search (not recursive).// [折半查找的前提]:// 1、待查找序列必须采用顺序存储结构。// 2、待查找序列必须是按关键字大小有序排列。// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    /// <summary>    /// The program.    /// </summary>    internal class Program    {        /// <summary>        /// Entry point into console application.        /// </summary>        public static void Main()        {            int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };            Console.WriteLine(BinarySearch(a, 6, 9));        }        /// <summary>        /// 在长度为n的有序数组a中查找值为key的元素(非递归查找)。        /// </summary>        /// <param name="a">        /// 待查找数组。        /// </param>        /// <param name="key">        /// 目标元素。        /// </param>        /// <param name="n">        /// 数组长度。        /// </param>        /// <returns>        /// 若查找到目标元素则返回该目标元素在数组中的下标;否则返回-1。        /// </returns>        private static int BinarySearch(int[] a, int key, int n)        {            int low = 0;            int high = n - 1;            while (low <= high)            {                int mid = (low + high) / 2;                if (a[mid] == key)                {                    return mid;                }                if (a[mid] < key)                {                    low = mid + 1;                }                else                {                    high = mid - 1;                }            }            return -1;        }    }}// Output:/*5*/
时间复杂度:O(log2n)           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值