Data Structures & AlgorithmsVery hard

Sort 7 8 3 12 5 6 15 4 by the NUMBER OF 1-BITS in binary, ascending; values with the same bit count are ordered by value ascending. Give the result.

  1. A. 3 4 5 6 7 8 12 15
  2. B. 15 7 3 5 6 12 4 8
  3. C. 4 8 3 5 6 12 7 15
  4. D. 8 4 12 6 5 3 7 15

Correct answer: C. 4 8 3 5 6 12 7 15

Use the key (popcount, value): popcount orders the groups and the value breaks ties within each group. Sorting by value alone, descending popcount, or reversing the tie-break gives other sequences.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic