折半查找 (跟据不同折半算法的多种实现)

       折半查找的基本思想
      
        折半查找(Binary Search)又叫二分查找,其基本思想是:在有序表中,取中
        间的记录作为比较对象,如果要查找记录的关键码等于中间记录的关键码,则查
        找成功;若要查找记录的关键码小于中间记录的关键码,则在中间记录的左半区
        继续查找;若要查找记录的关键码大于中间记录的关键码,则在中间记录的右半
        区继续查找。不断重复上述查找过程,直到查找成功,或有序表中没有所要查找
        的记录,查找失败。
       
       
        跟据不同的折半写法,来写下面的三种程序
       
        1、中心点为mid = (low + high) / 2的写法
        2、中心轴为int mid = (high - low) / 2 的写法
        3、递归的写法
       
        好了,多了就不说了。详细看代码吧。
       
        不过在开始之前,先假设我们的有序数为
       
         int[] ser=new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,18,50};

----------------------------------------------------------------------------------------------------------------------

         /// <summary>
        /// 中心点为mid = (low + high) / 2的写法
        /// </summary>
        /// <param name="S">有序数组</param>
        /// <param name="v"> 要查找的值</param>
        /// <returns></returns>
        private string Search(int[] S, int v)
        {
            int mid = 0;
            int high = S.Length - 1;
            int low = 0;
            while (low <= high)
            {
                mid = (low + high) / 2; //低边界+高边界再除以2 也就是取平均值,计算后的值x一定在low<=x<=high

                if (S[mid] == v)
                {
                    return S[mid].ToString();//如是当前值等于要找找的值,返回找到的值
                }
                else
                {
                    if (S[mid] > v)
                    {
                        high = mid - 1;//如果当前值比要查找的值大时,说明要查找的值在当前值的前面.
                    }
                    else
                    {
                        low = mid + 1;//否则说明要查找的值在当前值的后面.
                    }
                }
            }

            return "未找到";//如是经过上面的折腾都没有值返回,说明没有找到找的值
        }

 

 

--------------------------------------------------------------------------------------------------------------

 //<summary>
         //中心轴为int mid = (high - low) / 2 的写法
         //</summary>
         //<param name="S">有序数组</param>    
         //<param name="low">低边界</param>
         //<param name="high">高边界</param>
         //<param name="v"> 要查找的值</param>
         //<returns></returns>
        private string Search(int[] S, int low, int high, int v)
        {          

            while(low<=high)
            {
                int mid = (high - low) / 2;

                if (S[low+mid] == v)
                {
                    return "s[" +(low+ mid).ToString() + "]=" + S[low + mid].ToString()+"  v="+v.ToString();

                }
                else
                {
                    if (S[low+mid] > v)
                    {
                        high =low+mid - 1;
                    }
                    else
                    {
                        low =low+ mid + 1;                   
                    }               
                }           
            }

            return "未找到";
        }

 

---------------------------------------------------------------------------------------------------------

   /// <summary>
        ///  递归的写法
       /// </summary>
        /// <param name="S">有序数组</param>    
        /// <param name="low">低边界</param>
        /// <param name="high">高边界</param>
        /// <param name="v"> 要查找的值</param>
        /// <returns></returns>

  private string Search(int[] S, int v,int low,int high)
        {
            int mid = (low + high) / 2;
          
            if (low <= high)
            {
                if (S[mid] == v)
                {
                    return S[mid].ToString();
                }
                else
                {
                    if (S[mid] > v)
                    {
                        high = mid - 1;
                       
                    }
                    else
                    {
                        low = mid + 1;
                    }
                  return  Search(S, v, low, high);
                }
            }

            return "未找到";
        }

 

--------------------------------------------------------------------------------------

好了就写这么多吧。。。。。

 

 

 

 

 

posted @ 2011-11-24 17:37  kingNull  阅读(3356)  评论(0编辑  收藏  举报