ProgrammingModerate
A recurrence T(n) = 2 T(n/2) + n describes which algorithm, with which complexity?
- A. Merge sort, with O(n log n) time
- B. Binary search, with O(log n) time
- C. Bubble sort, with O(n^2) time
- D. Linear search, with O(n) time
Correct answer: A. Merge sort, with O(n log n) time
Each level splits the input in half and does linear work, which gives n log n by the master theorem.
More C Programming questions
- For a number n up to 10^18 read as a decimal string, what is the number of digits, computed with one pass?
- A program keeps a set of at most 10^5 ints and answers membership queries 10^5 times. Which structure is correct and fast enough?
- Which statement about finding the length of a null-terminated C string is correct?
- Which code gives the correct sum of the first n squares, for n up to 10^6, using a closed form in O(1)?
- Which operation on a dynamic array of n elements has O(1) amortised cost when using doubling?
- A loop does for (i = n; i > 0; i /= 2) for (j = 0; j < i; j++) work();. For n = 1024, how many times is work() called in total?