Question
Consider linear search again (see Exercise 2.1-3). How many elements of the input sequence need to be checked on the average, assuming that the element
Consider linear search again (see Exercise 2.1-3). How many elements of the input sequence need to be checked on the average, assuming that the element being searched for is equally likely to be any element in the array? How about in the worst case? What are the average-case and worst-case running times of linear search in notation? Justify your answers.
I understand that the average case is n/2 and that the worst case is n. I also know that the running times for both cases in notation is (n). However, I don't know how an answer of (n) is reached for both cases. Could someone provide a Cost Time Analysis next to each line of the pseudocode (shown below), show how T(n) is computed, and explain how (n) is found from T(n)?
This is the linear search pseudo code:
1 for i = 1 to A.length
2 if A[i] = v
3 return i
4 return nil
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started