算法 - 折半查找(C#)

/*
 * Recursive Binary Search - by Chimomo
 *
 * [折半查找的前提]:
 * 1、待查找序列必须采用顺序存储结构。
 * 2、待查找序列必须是按关键字大小有序排列。
 *
 * 时间复杂度:O(log2n)
 */
 
namespace RecursiveBinarySearch
{
    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="arr">
        /// 待查找数组。
        /// </param>
        /// <param name="x">
        /// 目标元素。
        /// </param>
        /// <param name="low">
        /// 数组元素下标的下界。
        /// </param>
        /// <param name="high">
        /// 数组元素下标的上界。
        /// </param>
        /// <returns>
        /// 若查找到目标元素则返回该目标元素在数组中的下标;否则返回-1。
        /// </returns>
        private static int BinarySearch(int[] arr, int x, int low, int high)
        {
            if (low > high)
            {
                return -1;
            }
 
            int mid = (low + high) / 2;
            if (x == arr[mid])
            {
                return mid;
            }
 
            return x < arr[mid] ? BinarySearch(arr, x, low, mid - 1) : BinarySearch(arr, x, mid + 1, high);
        }
    }
}
 
// Output:
/*
5
*/
/*
 * Non-Recursive Binary Search - by Chimomo
 *
 * [折半查找的前提]:
 * 1、待查找序列必须采用顺序存储结构。
 * 2、待查找序列必须是按关键字大小有序排列。
 *
 * 时间复杂度:O(log2n)
 */
 
namespace NonRecursiveBinarySearch
{
    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的有序数组arr中查找值为x的元素(非递归查找)。
        /// </summary>
        /// <param name="arr">
        /// 待查找数组。
        /// </param>
        /// <param name="x">
        /// 目标元素。
        /// </param>
        /// <param name="n">
        /// 数组长度。
        /// </param>
        /// <returns>
        /// 若查找到目标元素则返回该目标元素在数组中的下标;否则返回-1。
        /// </returns>
        private static int BinarySearch(int[] arr, int x, int n)
        {
            int low = 0;
            int high = n - 1;
            while (low <= high)
            {
                int mid = (low + high) / 2;
                if (arr[mid] == x)
                {
                    return mid;
                }
 
                if (arr[mid] < x)
                {
                    low = mid + 1;
                }
                else
                {
                    high = mid - 1;
                }
            }
 
            return -1;
        }
    }
}
 
// Output:
/*
5
*/

猜你喜欢

转载自blog.csdn.net/qq_24624539/article/details/91350491