algorithm - How many elements need to be checked on an average in linear search? -


question: consider linear search. how many elements of input sequence need checked on average, assuming element being searched equally element in array?

how solve this? need take case element not present in sequence? case n elements needs checked. total no. of cases (n + 1). therefore average no. of elements checked = (1 + ... + n + n) / (n + 1). answer correct?

distinguishing 2 cases element in array (successful search) , not (unsuccessful search).

for successful search average (1 + 2 + ... + n) / n = (n + 1) / 2.

for unsuccessful search average n.

the total average depends on mix of successful vs unsuccessful searches. example if of searches unsuccessful close n. if searches successful (this seems meant question) average (n + 1) / 2.

if probability of successful search p, , therefore unsuccessful search 1-p, average of p * (n+1) / 2 + (1-p) * n.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -