C# 二分查找

        /// <summary>
        /// 二分查找
        /// </summary>
        /// <param name="arr">数组</param>
        /// <param name="low">开始索引 0</param>
        /// <param name="hight">结束索引 </param>
        /// <param name="key">要查找的对象</param>
        /// <returns>对象所在的索引位置</returns>
        public static int BinarySearch(int[] arr ,int low, int hight,int key)
        {
            int mid = (low + hight) / 2;
            if (low>hight)
            {
                return -1;
            }
            else
            {
                if (arr[mid] == key)
                {
                    return mid;
                }
                else if(arr[mid] > key)
                {
                    return BinarySearch(arr, low, mid - 1, key);
                }
                else
                {
                    return BinarySearch(arr, mid + 1, hight, key);
                }
            }

        }



    int[] y = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            int rr = BinarySearch(y, 0, y.Length - 1, 5);
            Console.Write(rr);

            Console.Read();

C# 二分查找

上一篇:node js v0.10.35 Windows Xp安装


下一篇:简单的飞信API (免费短信.飞信) 自制