GATE CSE
First time here? Checkout the FAQ!
x
+2 votes
82 views
Given an array of 1,000,000 distinct integers you are going to perform 100,000 searches, one at a time, using the linear search algorithm. Each element you are searching for is in the array. What is the expected, total number of comparisons performed by the 100,000 linear searches?

Please explain

Given Ans: 5x10^10 + 5x10^4
asked in Algorithms by Veteran (11.7k points)   | 82 views

1 Answer

+6 votes
Best answer
Expected number of searches for a single linear search of $n$ elements

$$= 1.\frac{1}{n} + 2.\frac{1}{n} + \dots n.\frac{1}{n}$$

as elements are distinct and the probability of any element being equal to the searched element is $\frac{1}{n}$ as element is guaranteed to be present

$= \frac{n+1}{2}$.

Here, $n = 1,000,000$. So, expected number of comparisons $= 1,000,001/2$

Expected no. of comparisons for 100,000 such searches $= 100,000 \times 1,000,001/2 \\=5 \times 10^{10} + 5 \times 10^4$
answered by Veteran (273k points)  
selected by
good question and good explaination ,thanks :)
Top Users Jan 2017
  1. Debashish Deka

    8322 Points

  2. sudsho

    5166 Points

  3. Habibkhan

    4718 Points

  4. Vijay Thakur

    4468 Points

  5. Bikram

    4420 Points

  6. saurabh rai

    4212 Points

  7. Arjun

    4072 Points

  8. santhoshdevulapally

    3732 Points

  9. Sushant Gokhale

    3514 Points

  10. GateSet

    3336 Points

Monthly Topper: Rs. 500 gift card

19,158 questions
24,065 answers
52,872 comments
20,288 users