ProgrammingHard
A binary search over n = 10^6 sorted records, in the worst case, takes how many loop iterations?
- A. 10
- B. 19
- C. 20
- D. 1000000
Correct answer: C. 20
The range halves each step, and 2^20 = 1048576 is the first power of two above 10^6, so 20 iterations are enough.
More C Programming questions
- A sieve of Eratosthenes up to n = 100 marks composites by starting each prime p at pp, and only for pp <= n. How many marking operations…
- With fib(1) = fib(2) = 1, what is the largest n for which fib(n) fits in a signed 64-bit long long?
- A loop appends one character to a string with strcat(s, "x") for n = 10^5 times. Each strcat calls strlen on the whole string first. What…
- for (i = 0; i < strlen(s); i++) process(s[i]); with a string of length 5. How many times is strlen called?
- Which C code is correct and fast enough for finding whether a sorted array a of n = 2 x 10^5 ints has two elements summing to t?
- For N = 10^6 integers in the range 0 to 10^6, which sorting method is correct and fast enough?