Question

In: Computer Science

101 students won iPhone. The winners listed in sorted order. How many iterations will it take...

101 students won iPhone. The winners listed in sorted order. How many iterations will it take for a binary search to find a particular name on the list, in the worst case?

Solutions

Expert Solution

The worst case time complexity for this case will be O(log n) where n is the number of elements in the array which is 101 so it can also be written as O(log2 101) this logarithm has a base 2 since at each iteration in a binary search the array is divided into two halves and then by comparing the middle element with our target element we check whether they are equal or not anf if they are not equal we chose any one of the halves

so the number of iterations in worst case binary search will be :

log2 101=6.65 which can be rounded of to 7


   int mid = (first + last)/2;  //first stores the first element and last stores the last elemnt
   while( first <= last ){  
      if ( arr[m] < key ){  // checking whether our target element is greater to middle element if yes then the second half of array is chosen
        first = m + 1;     
      }else if ( arr[m] == key ){  // checking whether middle element is equal to target element
        System.out.println("Element is found at index: " + m);  
        break;  
      }else{  
         last = m - 1;  // else first half of the array is chosen
      }  
      m = (first + last)/2;  
   }  
   if ( first > last ){  // it means the whole array is checked
      System.out.println("Element is not found!");  
   }  
 }  

Related Solutions

3. A local college is studying the number of credit hours students take and how many...
3. A local college is studying the number of credit hours students take and how many total minutes they spend reading for fun/pleasure (e.g., non-class-related materials). They don’t know whether to expect that studentswill read more or less when enrolled in a lot of classes/hours or fewer hours. They study 68 students and find a correlation of -0.30. (A) State the decision rule for the 0.05 significance level. (6) Reject H0 if t > _________ (B) Compute the value of...
. In order to determine how many hours per week freshmen college students watch television, a...
. In order to determine how many hours per week freshmen college students watch television, a random sample of 25 students was selected. It was determined that the students in the sample spent an average of 19.5 hours with a sample standard deviation of 4.2 hours watching TV per week. Please answer the following questions: (a) Provide a 95% confidence interval estimate for the average number of hours that all college freshmen spend watching TV per week. (b) Assume that...
1. Any “case” statement can be written as an “if” statement. True False 2.How many iterations...
1. Any “case” statement can be written as an “if” statement. True False 2.How many iterations does the following “loop” statement have? n = 1 loop do n = n + 1 puts “hey" next unless n = 10 break end 0 7 This loop is syntactically incorrect. 9 8 3.The following two conditional expressions are equivalent outcome wise: n = 1 begin puts n end while n < 1 n = 1 while n < 1 puts n end...
Given a list of 4096 sorted values, about how many comparisons can you expect to be...
Given a list of 4096 sorted values, about how many comparisons can you expect to be performed to look for a value that's not in the list using the bubble sort algorithm?
How many elements of order 2 are there in S5 and in S6? How many elements...
How many elements of order 2 are there in S5 and in S6? How many elements of order 2 are there in Sn? (abstract algebra)
Many high school students take the AP tests in different subject areas. In 2007, of the...
Many high school students take the AP tests in different subject areas. In 2007, of the 144,796 students who took the biology exam 84,199 of them were female. In that same year, of the 211,693 students who took the calculus AB exam 102,598 of them were female ("AP exam scores," 2013). Estimate the difference in the proportion of female students taking the biology exam and female students taking the calculus AB exam using a 90% confidence level. Considering it is...
Many high school students take the AP tests in different subject areas. In 2007, of the...
Many high school students take the AP tests in different subject areas. In 2007, of the 143044 students who took the AP biology exam 76712 of them were female. In that same year, of the 211993 students who took the AP calculus AB exam 100106 of them were female. Is there enough evidence to show that the proportion of AP biology exam takers who are female is higher than the proportion of AP calculus AB exam takers who are female?...
Many high school students take the AP tests in different subject areas. In 2007, of the...
Many high school students take the AP tests in different subject areas. In 2007, of the 144,796 students who took the biology test 84,199 of them were female. In that same year, of the 211,693 students who took the calculus AB test 102,598 of them were female. Is there enough evidence to show that the proportion of female students taking the biology test is higher than the proportion of female students taking the calculus AB test? Test at the 5%...
Many high school students take the SAT's twice; once in their Junior year and once in...
Many high school students take the SAT's twice; once in their Junior year and once in their Senior year. The Senior year scores (x) and associated Junior year scores (y) are given in the table below. This came from a random sample of 35 students. Use this data to test the claim that retaking the SAT increases the score on average by more than 25 points. Test this claim at the 0.10 significance level. (a) The claim is that the...
- How many years will it take for $100,000 to grow to $500,000 if it is...
- How many years will it take for $100,000 to grow to $500,000 if it is invested at an annual interest rate of 9.15%? Round to the nearest 0.01. - You are offered a line of credit with a quoted interest rate of 20% with daily compounding. What is your effective annual interest rate? Round to the nearest 0.01%.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT