Question: Linear search The code below searches an unsorted array, A, for a given value. It returns the index of the value if it is present.

 Linear search The code below searches an unsorted array, A, for

Linear search The code below searches an unsorted array, A, for a given value. It returns the index of the value if it is present. Otherwise it returns -1. Suppose before we call linearSearch we randomly permute (i.e., reorder) the elements in A. In the following problem, we will find the expected asymptotic runtime of linearSearch. What is the best case asymptotic runtime of linearSearch on an array of N elements? What index must val be located at to elicit this runt ime? What is the worst case asymptotic runtime of linearSearch on an array of N elements? What index must val be located at to elicit this runtime? Let X_J be the random variable which, given an permutation of the elements in our array, is 1 if index A[J] will be checked in our linearSearch for that permutation and 0 otherwise. What is the expected value of X_J? Let X be the random variable which, given an permutation of the elements in our array, is the number of elements of A which will be checked in our linearSearch for that permutation. Define X using N copies of the random variable defined in the previous step. Compute the expected value of X for using the linearity of expectation. Given this, what is the expected asymptotic runtime of linearSearch

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!