Same results, different costs

An IntSet abstract data type has two operations: insert(x) adds the integer x if it is not already a member and otherwise changes nothing, and contains(x) returns true if x is a member and false otherwise. Two implementations meet this specification.

  • Implementation A stores the members in an unsorted array. insert(x) appends x to the end if it is not already present. contains(x) examines elements from index 0 onward and stops when it finds x or reaches the end of the array.
  • Implementation B stores the members in an array sorted in ascending order. insert(x) places a new member in its sorted position. contains(x) examines elements from index 0 onward and stops at the first element greater than or equal to x, returning true only if that element equals x.

Starting from an empty set, each implementation receives insert(40), insert(15), insert(72), insert(8), insert(15) and insert(33), then contains(33), contains(50) and contains(8).


Counting only the elements examined during the three contains calls, give the total number of elements examined by implementation A and by implementation B, each as a whole number. Also give the result returned by contains(50), as true or false.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Abstract Data Types (ADTs) Questions: