views:

64

answers:

1

Consider a disk file containing 100 records a. How many comparisons would be required on the average to find a record using sequential search, if the record is known to be in the file?

I figured out that this is 100/2 = 50.

b. If the record has a 68% probability of being in the file, how many comparisons are required on average?

This is the part I'm having trouble with. At first I thought it was 68% * 50, but then realized that was wrong after thinking about it. Then I thought it was (100% - 68%) * 50, but I still feel that that is wrong. Any hints?

+4  A: 

I'd break it down like this, into a weighted average.

A 68% chance of it being in the file; under these circumstances an average of 50 comparisons will be needed from your result in part I.

A 32% chance of the record NOT being in the file; under these circumstances you will need to look through every record, i.e 100 comparisons.

0.68*50 + 0.32*100 = 66 comparisons on average.

But it has been a while since I took a course on probability...

Peter
Wouldn't it be 0.32*99, since for 100 records 99 comparisons are made, not 100.
Phenom
How would you only have to make 99 comparisons? If there are 100 records, and you don't know for certain that the one you are searching for exists, would you not have to look at every single record using a sequential search?
Peter
I guess I got confused by the word "comparison," thinking that you were comparing the numbers to each other.
Phenom