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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!