Name a comparison-based sorting algorithm with average time complexity O(n log n).

Study for the CodeHS AP Computer Science Principles (CSP) Exam. Prepare with flashcards and multiple choice questions, each question comes with hints and explanations. Get ready for success!

Multiple Choice

Name a comparison-based sorting algorithm with average time complexity O(n log n).

Explanation:
Sorting by comparing elements and using a strategy that splits the data and merges results yields efficient performance. Merge sort does exactly that: it splits the list in two, recursively sorts each half, and then merges the two sorted halves by repeatedly comparing the smallest remaining elements. This divide-and-conquer process leads to a recurrence T(n) = 2T(n/2) + O(n), which solves to O(n log n) on average. Among the options, this is the one that uses comparisons and achieves the desired O(n log n) average time. The other common sorts either run in O(n^2) on average because they swap or shift many elements, or (in the case of counting sort) aren’t based on comparisons and have a time complexity of O(n + k), depending on the range of input values.

Sorting by comparing elements and using a strategy that splits the data and merges results yields efficient performance. Merge sort does exactly that: it splits the list in two, recursively sorts each half, and then merges the two sorted halves by repeatedly comparing the smallest remaining elements. This divide-and-conquer process leads to a recurrence T(n) = 2T(n/2) + O(n), which solves to O(n log n) on average. Among the options, this is the one that uses comparisons and achieves the desired O(n log n) average time. The other common sorts either run in O(n^2) on average because they swap or shift many elements, or (in the case of counting sort) aren’t based on comparisons and have a time complexity of O(n + k), depending on the range of input values.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy