C语言数据结构中二分查找递归非递归实现并分析

  

<强> C语言数据结构中二分查找递归非递归实现并分析

  

<强>前言:

  

二分查找在有序数列的查找过程中算法复杂度低,并且效率很高。因此较为受我们追捧。其实二分查找算法,是一个很经典的算法。但是呢,又容易写错。因为总是考虑不全边界问题。
  

  

用非递归简单分析一下,在编写过程中,如果编写的是以下的代码:

        # include   # include   使用名称空间性病;      int binaty_search (int *加勒比海盗,size_t n, int x)   {   断言(arr);   int左=0;   int右=n - 1;      而(左& lt;=)   {   int中期=(左+右)/2;   如果(x & lt;加勒比海盗[中])   {   正确的=mid-1;   }   else if (x比;加勒比海盗[中])   {   左=中期+ 1;   }   其他的   返回中期;   }   返回1;   }      int main ()   {   arr int []={0, 1, 2, 3, 4, 5, 6, 7, 8, 9};   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 0) & lt; & lt;endl;   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 1) & lt; & lt;endl;   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 2) & lt; & lt;endl;   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 3) & lt; & lt;endl;   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 4) & lt; & lt;endl;   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 5) & lt; & lt;endl;   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 6) & lt; & lt;endl;   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 7) & lt; & lt;endl;   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 8) & lt; & lt;endl;   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 9) & lt; & lt;endl;   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 10) & lt; & lt;endl;   返回0;   }      之前      

那么我们可以简单分析一下:
  

  

 C语言数据结构中二分查找递归非递归实现并分析

  

  

如果是以下这样的代码实现:

        # include   # include   使用名称空间性病;      int binaty_search (int *加勒比海盗,size_t n, int x)   {   断言(arr);   int左=0;   int右=n;      而(左& lt;右)   {   int中期=(左+右)/2;   如果(x & lt;加勒比海盗[中])   {   正确的=中期;   }   else if (x比;加勒比海盗[中])   {   左=中期+ 1;   }   其他的   返回中期;   }   返回1;   }   int main ()   {   arr int []={0, 1, 2, 3, 4, 5, 6, 7, 8, 9};   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 0) & lt; & lt;endl;   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 1) & lt; & lt;endl;   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 2) & lt; & lt;endl;   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 3) & lt; & lt;endl;   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 4) & lt; & lt;endl;   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 5) & lt; & lt;endl;   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 6) & lt; & lt;endl;   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 7) & lt; & lt;endl;   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 8) & lt; & lt;endl;   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 9) & lt; & lt;endl;   cout & lt; & lt;binaty_search (arr sizeof (arr)/sizeof (int), 10) & lt; & lt;endl;   返回0;   }      之前      

那么可以简单分析一下为:
  

  

 C语言数据结构中二分查找递归非递归实现并分析

  

同样,递归实现的条件也分为两种,我就只演示一种,代码如下:

        # include   # include   使用名称空间性病;      int binaty_srarch (int *加勒比海盗,int x, int, int)   {   断言(arr);   int中期;   如果(左& lt;=)   {   中期=(左+右)/2;   如果(arr(中期)==x)   {   返回中期;   }   其他的   如果(x & lt;加勒比海盗[中])   {   返回binaty_srarch (arr x,左、右- 1);   }   else if (x> arr[中])   {   返回binaty_srarch (arr x,左+ 1,右);   }   }   返回1;   }      int main ()   {   arr int []={0, 1, 2, 3, 4, 5, 6, 7, 8, 9};   cout & lt; & lt;binaty_srarch(加勒比海盗,0,0,(sizeof (arr)/sizeof (int)) - 1) & lt; & lt;endl;   cout & lt; & lt;binaty_srarch(加勒比海盗1 0,(sizeof (arr)/sizeof (int)) - 1) & lt; & lt;endl;   cout & lt; & lt;binaty_srarch(加勒比海盗2 0,(sizeof (arr)/sizeof (int)) - 1) & lt; & lt;endl;   cout & lt; & lt;binaty_srarch(加勒比海盗3 0,(sizeof (arr)/sizeof (int)) - 1) & lt; & lt;endl;   cout & lt; & lt;binaty_srarch(加勒比海盗4 0,(sizeof (arr)/sizeof (int)) - 1) & lt; & lt;endl;   cout & lt; & lt;binaty_srarch(加勒比海盗5 0,(sizeof (arr)/sizeof (int)) - 1) & lt; & lt;endl;   cout & lt; & lt;binaty_srarch (arr 6 0, (sizeof (arr)/sizeof (int)) - 1) & lt; & lt;endl;   cout & lt; & lt;binaty_srarch (arr 7 0, (sizeof (arr)/sizeof (int)) - 1) & lt; & lt;endl;   cout & lt; & lt;binaty_srarch (arr 8 0, (sizeof (arr)/sizeof (int)) - 1) & lt; & lt;endl;   cout & lt; & lt;binaty_srarch (arr 9 0, (sizeof (arr)/sizeof (int)) - 1) & lt; & lt;endl;   cout & lt; & lt;binaty_srarch (arr 10 0, (sizeof (arr)/sizeof (int)) - 1) & lt; & lt;endl;      返回0;   }      

C语言数据结构中二分查找递归非递归实现并分析