5

I'm very lost on finding average case complexity, just pulling a random problem...like.

For a sentinel sequential search, find the average case if the probability is 0 <= p <= 1.

I get the worst case would be O(n+1) as there would be n elements in the array plus the extra element for the key you add on at the end. Best case would be just O(1) if you find it immediately.

I don't really want the answer...but more how...if I just wanted the answer I guess I'd just look a solution manual.

Carson Wood
  • 1,174
  • 3
  • 14
  • 23
  • What exactly is your question? – SegFault Feb 06 '16 at 22:35
  • The part about the probability is nearly irrelevant. It just means "it's unknown whether the search is successful or not". So the answer is O(n+1), or maybe O(n+2) if you count the comparison in the ternary at the end. Regardless, the *important* part is that the performance of sentinel search is "linear time", that is, it scales linearly with the number of elements. As opposed to quadratic/exponential/etc. time. – Aaron Brager Feb 06 '16 at 23:10
  • What do you mean by "the probability is 0 <= p <= 1"? – Paul Hankin Feb 07 '16 at 00:56

1 Answers1

2

You're right that "average case complexity" requires careful definition of both the algorithm and the set of possible inputs.

The number of comparisons needed to search a linear list of integers provides an example.

If the input search key can be any arbitrary integer, then the average result is that the entire list will be searched (requiring n+1 comparisons to find the sentinel) because there are infinitely many integers and only finitely many elements in the array. Only a finite number of inputs will require less than n+1 comparisons, but infinitely many will cause n+1.

If the analysis on the other hand is the average number of comparisons when the search key is (uniformly) randomly selected from the elements in the array (and those elements contain no repeats), then the average of possible outcomes will be the number of comparisons when the searched-for item is first in the list plus the number when it's second in the list, etc. up to the n'th item, all divided by the number of outcomes, which is n. In other words,

(1 + 2 + ... n) / n = n(n+1)/(2n) = (n + 1) / 2

Here's a sanity check: Let n=1. Then the formula says the average number of comparisons to find the element in the 1-element list is one. That's obviously correct.

A final note is that the way you've posed the questions shows you should study the definition of big-O. O(n) is the same as O(n+1). And big-O is always an expression of an upper bound on whatever's measured. Expressing an upper bound on an average is often not appropriate because the average case analysis normally provides a lower bound as well. The (n+1)/2 comparisons above would best be expressed as \Theta(n) in the average case.

Gene
  • 46,253
  • 4
  • 58
  • 96
  • "And big-O is always an expression of "worst" case behavior in that it states only an upper bound."- We can express the average case's running time in Big-O. Perpetuating the general notion that Big-O is always used as an expression of worst case is sometimes misleading for a newbie. – Aravind Feb 07 '16 at 05:17
  • Thank you, that makes a lot more sense! – Carson Wood Feb 07 '16 at 20:47
  • What if there is repeats? If the search key takes value in a finite domain of size d, it seems that the average complexity is constant, but I have hard time formalizing this. – scand1sk May 25 '21 at 08:53