ProgrammingModerate
A linear search over an array of length n finds the value only at the last position. How many comparisons does it make?
- A. n
- B. n - 1
- C. log n
- D. n + 2
Correct answer: A. n
Every element is compared once before the match at the end, giving n comparisons.
More C Programming questions
- Bubble sort with a swapped flag stops early. On an already sorted array of length n, how many passes run?
- A recursive binary search on n = 1000 sorted values reaches what maximum recursion depth?
- Merge sort on n = 2^20 elements has what recursion depth?
- An algorithm does O(n) work on each of the n levels of a recursion that removes one element per level. Which total is correct?
- A loop calls strlen(s) on a fixed string of length 10^4 once per character of the same string. How many characters are read in total?
- A function computes the sum of 1 to n by recursion with depth n. For n = 10^6, what is the main risk?