Search code examples
searchsequential

sequential search homework question


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?


Solution

  • 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...