ProgrammingModerate

Which operation makes a sorted array of n elements support lookup in O(log n) and insertion in O(n)?

  1. A. A hash table with chaining, since it is always sorted
  2. B. A singly linked list, because lookup is O(1) on a sorted list
  3. C. A stack, which gives O(log n) insertion
  4. D. A sorted array with binary search for lookup and memmove for insertion

Correct answer: D. A sorted array with binary search for lookup and memmove for insertion

The array can be searched by bisection. Inserting in the middle shifts elements, which costs O(n) per insert.

More C Programming questions

Exams that ask this topic