c# 为什么二元搜索优于三元搜索

        在计算机科学中,二元搜索(Binary Search)和三元搜索(Ternary Search)是两种查找算法,用于在有序数组或列表中查找目标元素的位置。它们之间的主要区别在于每次查找时如何划分搜索范围的方式。

        1、二元搜索(Binary Search):二元搜索是一种通过将搜索范围划分为两部分,然后确定目标元素位于左侧或右侧来缩小搜索范围的算法。在每一步中,算法将目标值与数组中间元素进行比较,如果中间元素等于目标值,则找到目标,如果中间元素大于目标值,则在左半部分继续搜索,如果中间元素小于目标值,则在右半部分继续搜索。通过不断划分搜索范围,最终找到目标值或确定目标不存在。

        2、三元搜索(Ternary Search):三元搜索是一种通过将搜索范围划分为三部分,然后确定目标元素位于左侧、中间还是右侧来缩小搜索范围的算法。在每一步中,算法将目标值与数组中两个划分点的值进行比较,根据比较结果将搜索范围划分为三个部分。然后根据比较结果选择左侧、中间或右侧部分进行下一步搜索。通过这种方式,可以更快地收敛到目标值。

        根据上面的定义,二元搜索是将搜索范围划分为两部分,而三元搜索是将搜索范围划分为三部分。在某些情况下,三元搜索可能更快地找到目标值,但也需要更多的比较操作。
 

下面是一个简单的 C# 递归二分查找函数:

// A recursive binary search function. It returns location of x in
// given array arr[l..r] is present, otherwise -1
static int binarySearch(int []arr, int l, int r, int x)
{
   if (r >= l)
   {
        int mid = l + (r - l)/2;
  
        // If the element is present at the middle itself
        if (arr[mid] == x)  return mid;
  
        // If element is smaller than mid, then it can only be present
        // in left subarray
        if (arr[mid] > x) return binarySearch(arr, l, mid-1, x);
  
        // Else the element can only be present in right subarray
        return binarySearch(arr, mid+1, r, x);
   }
  
   // We reach here when element is not present in array
   return -1;

 
// This code is contributed by gauravrajput1 

以下是一个简单的递归三元搜索函数: 

// A recursive ternary search function. 
// It returns location of x in given array
// arr[l..r] is present, otherwise -1
static int ternarySearch(int []arr, int l, 
                         int r, int x)
{
   if (r >= l)
   {
        int mid1 = l + (r - l) / 3;
        int mid2 = mid1 + (r - l) / 3;
   
        // If x is present at the mid1
        if (arr[mid1] == x)  return mid1;
   
        // If x is present at the mid2
        if (arr[mid2] == x)  return mid2;
   
        // If x is present in left one-third
        if (arr[mid1] > x) 
            return ternarySearch(arr, l, mid1 - 1, x);
   
        // If x is present in right one-third
        if (arr[mid2] < x) 
            return ternarySearch(arr, mid2 + 1, r, x);
   
        // If x is present in middle one-third
        return ternarySearch(arr, mid1 + 1, 
                                  mid2 - 1, x);
   }
    
   // We reach here when element is
   // not present in array
   return -1;
}
 
// This code is contributed by gauravrajput1

上述两者中哪一个在最坏情况下比较较少? 

        从第一眼看上去,三元搜索似乎进行了较少的比较次数,因为它进行了 Log 3 n 次递归调用,但二分搜索进行了 Log 2 n 次递归调用。让我们仔细看看。 
以下是二分查找最坏情况下计算比较的递归公式。 

   T(n) = T(n/2) + 2, T(1) = 1

以下是三元搜索最坏情况下计算比较的递归公式。

  T(n) = T(n/3) + 4, T(1) = 1

在二分查找中,最坏情况下有 2Log 2 n + 1 次比较。在三元搜索中,最坏情况下有 4Log 3 n + 1 次比较。 

二元搜索的时间复杂度 = 2clog 2 n + O(1)
三元搜索的时间复杂度 = 4clog 3 n + O(1)

        因此,三元搜索和二元搜索的比较归结为表达式 2Log 3 n 和 Log 2 n的比较。 2Log 3 n的值可以写为 (2 / Log 2 3) * Log 2 n 。由于 (2 / Log 2 3)的值大于 1,因此在最坏情况下,三元搜索比二元搜索进行更多的比较。 

  • 35
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值