简体   繁体   English

O(1)和O(n)之间的线性搜索差异

[英]Linear Search difference between O(1) and O(n)

I am confused with the following two cases in linear search. 我对线性搜索中的以下两种情况感到困惑。 Let me write the linear search algorithm here first. 首先让我写线性搜索算法。

Pseudo code for linear search 线性搜索的伪代码

input: unsorted array A & key 输入:未排序的数组A和键

output: index i such that A[i] = key 输出:索引i使得A [i] =键

LinearSearch(A,low,high,key) 
if high < low    
   return NOT_Found
if A[low] = key
   return low
return LinearSearch(A,low+1,high,key)

The complexity is O(n) in most of the book. 在大多数书中,复杂度为O(n)。 What is the difference between 之间有什么区别

  1. Best-case runtime for linear search if key is present in the array [Ans: O(1)] 如果数组中存在键,则为线性搜索的最佳情况运行时[Ans:O(1)]
  2. Best-case runtime for linear search if key isn't present in the array [Ans: O(n)] 如果数组中不存在键,则为线性搜索的最佳情况运行时[Ans:O(n)]

What is the reason that when the key is present in the array, the complexity will reduce to O(1)? 当键存在于数组中时,复杂度降低到O(1)的原因是什么? Thank you very much! 非常感谢你!

Your question is based on the best case. 您的问题基于最佳情况。 The best case would be that your key is on the first position (O(1)). 最好的情况是您的密钥位于第一个位置(O(1))。 More practical would be the average case which is in both cases O(n) 更实际的是在两种情况下均为O(n)的平均情况

What is the reason that when the key is present in the array, the complexity will reduce to O(1)? 当键存在于数组中时,复杂度降低到O(1)的原因是什么?

The presence of the key in the array makes a difference since consider the two cases: 考虑到以下两种情况,数组中键的存在会有所不同:

  • The key exists in the array : then your running time in best case is O(1) , which means that you will have to move constant number of times in order to find it (eg it will be the 2nd element, the 10th element, the 1st element generally sth constant). 密钥存在于数组中 :那么最好的运行时间是O(1) ,这意味着您必须移动恒定的次数才能找到它(例如,它将是第二个元素,第十个元素,第一个元素通常是第一个常数)。 If the key exists then the worst case would be that it is in O(n) position in the array so you need at least Ω(n) steps and of course maximum steps is to search all n positions in array so O(n) which proves worst case Θ(n) . 如果键存在,则最坏的情况是它在数组中的O(n)位置,因此您至少需要Ω(n)步,当然最大步数是在数组中搜索所有n个位置,因此O(n)这证明了最坏情况Θ(n)

  • The key doesn't not exist : In this case given an array and a key that does not exist in the array your linear search algorithm attempting to find it he will have to check all elements in order to find it because the algorithm has two termination conditions: the key is found or the array has ended. 密钥不存在 :在这种情况下,给定一个数组和该数组中不存在的密钥,您的线性搜索算法试图找到它,他将必须检查所有元素才能找到它,因为该算法有两个终止条件:找到密钥或数组已结束。 Of courses the key in this case will not be found and the algorithm will terminate after checking all elements. 当然,在这种情况下将不会找到密钥,并且算法将在检查所有元素后终止。 An observation here is that if the key does not exist the algorithm will do exactly O(n) steps both in best or worst case, if the key does not exist best and worst cases are the same since in both you search whole array!! 这里的观察结果是,如果键不存在,则算法在最佳或最差情况下都将精确执行O(n)步骤,如果键不存在最佳和最坏情况,则算法相同(因为您都在搜索整个数组!)

In both cases as explained the worst case is θ(n) . 在上述两种情况下,最坏的情况是θ(n)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM