Counting Sort is a linear-time sorting algorithm designed for sorting arrays of integers where the range of input values is not significantly larger than the number of elements to be sorted. Unlike comparison-based sorts, it uses the actual values of the elements to determine their positions in the output array.
The algorithm works by first determining the range of the input elements, typically by finding the maximum value. It then creates a count array to store the frequency of each distinct element in the input. After populating the count array, it is modified to reflect the cumulative counts, which helps in placing elements in their correct sorted positions.
For example, given an array [4, 2, 2, 8, 3, 3, 1]:
– The count array might look like [0, 1, 2, 2, 1, 0, 0, 0, 1] for values up to 8.
– Using the cumulative counts, the sorted array becomes [1, 2, 2, 3, 3, 4, 8].
This method is stable and efficient, with a time complexity of O(n + k), where n is the number of elements and k is the range of input values. It excels in scenarios with non-negative integers and a limited range, but it is less suitable for floating-point numbers or large ranges due to high space requirements.
Table of Contents
- Part 1: Create A Counting Sort Quiz in Minutes Using AI with OnlineExamMaker
- Part 2: 20 Counting Sort Quiz Questions & Answers
- Part 3: AI Question Generator – Automatically Create Questions for Your Next Assessment

Part 1: Create A Counting Sort Quiz in Minutes Using AI with OnlineExamMaker
When it comes to ease of creating a Counting Sort skills assessment, OnlineExamMaker is one of the best AI-powered quiz making software for your institutions or businesses. With its AI Question Generator, just upload a document or input keywords about your assessment topic, you can generate high-quality quiz questions on any topic, difficulty level, and format.
Overview of its key assessment-related features:
● AI Question Generator to help you save time in creating quiz questions automatically.
● Share your online exam with audiences on social platforms like Facebook, Twitter, Reddit and more.
● Instantly scores objective questions and subjective answers use rubric-based scoring for consistency.
● Simply copy and insert a few lines of embed codes to display your online exams on your website or WordPress blog.
Automatically generate questions using AI
Part 2: 20 Counting Sort Quiz Questions & Answers
or
Question 1:
What is the primary advantage of Counting Sort over comparison-based sorting algorithms?
A. It has a lower space complexity.
B. It can sort in linear time for a fixed range of input values.
C. It works well with floating-point numbers.
D. It requires fewer comparisons.
Answer: B
Explanation: Counting Sort achieves O(n + k) time complexity, where n is the number of elements and k is the range of input values, making it linear when k is not much larger than n, unlike comparison-based sorts which are at least O(n log n).
Question 2:
In Counting Sort, what does the counting array store?
A. The sorted elements.
B. The frequency of each distinct element.
C. The positions of elements in the original array.
D. The cumulative sum of elements.
Answer: B
Explanation: The counting array keeps track of the frequency of each value in the input array, which is used to determine the positions of elements in the output array.
Question 3:
What is the time complexity of Counting Sort in the best case?
A. O(1)
B. O(n)
C. O(n log n)
D. O(n + k)
Answer: D
Explanation: Counting Sort’s time complexity is O(n + k), where n is the number of elements and k is the range of input values, and this holds true regardless of the input order, as it performs a fixed number of operations.
Question 4:
Counting Sort is most efficient when:
A. The input array is already sorted.
B. The range of input values is small compared to the number of elements.
C. The array contains negative numbers.
D. The array has a large number of duplicates.
Answer: B
Explanation: When the range of input values (k) is not significantly larger than the number of elements (n), the algorithm runs in linear time, making it efficient for such cases.
Question 5:
Which of the following is NOT a step in the Counting Sort algorithm?
A. Building the counting array.
B. Computing the cumulative count.
C. Performing a binary search on the array.
D. Placing elements in the output array.
Answer: C
Explanation: Binary search is not part of Counting Sort; the algorithm uses the counting array to directly determine positions, without any searching.
Question 6:
If an array has elements from 0 to 100 and contains 50 elements, what is the space complexity of Counting Sort?
A. O(1)
B. O(n)
C. O(k)
D. O(n + k)
Answer: D
Explanation: Counting Sort requires an auxiliary array of size k+1 (for values 0 to 100), plus space for the output array of size n, resulting in O(n + k) space complexity.
Question 7:
Can Counting Sort be used to sort strings?
A. Yes, directly.
B. No, only for numbers.
C. Yes, if strings are of fixed length and mapped to integers.
D. No, it requires floating-point numbers.
Answer: C
Explanation: Counting Sort can be adapted for strings by mapping them to integer keys, such as using radix sort, but it isn’t directly applicable without conversion.
Question 8:
What happens if the input array contains duplicate elements in Counting Sort?
A. Duplicates are ignored.
B. Duplicates are placed consecutively in the output.
C. The algorithm fails.
D. Duplicates require extra space.
Answer: B
Explanation: Counting Sort handles duplicates by maintaining their frequencies in the counting array, ensuring they are placed in the correct order in the output array.
Question 9:
In Counting Sort, the output array is constructed by:
A. Sorting the counting array first.
B. Using the cumulative counts to place elements in order.
C. Reversing the input array.
D. Merging with another array.
Answer: B
Explanation: The cumulative sum in the counting array helps determine the exact positions for each element in the output array, ensuring the array is sorted.
Question 10:
What is the worst-case time complexity of Counting Sort?
A. O(n^2)
B. O(n log n)
C. O(n + k)
D. O(k log k)
Answer: C
Explanation: Even in the worst case, Counting Sort runs in O(n + k) time, as long as k is reasonable, due to its non-comparative nature.
Question 11:
Counting Sort assumes that:
A. All elements are unique.
B. The input is already sorted.
C. The input elements are integers within a known range.
D. The array is of even length.
Answer: C
Explanation: Counting Sort requires the input to be integers (or mappable to integers) within a specific range to build the counting array effectively.
Question 12:
If k (range of input) is much larger than n (number of elements), Counting Sort becomes:
A. More efficient than quicksort.
B. Less efficient due to high space usage.
C. Ideal for large datasets.
D. Faster than merge sort.
Answer: B
Explanation: When k >> n, the space and time for the counting array dominate, making it inefficient compared to other sorting algorithms like quicksort or mergesort.
Question 13:
In a modified version of Counting Sort for negative numbers, what must be done?
A. Shift the numbers to make them positive.
B. Ignore negative numbers.
C. Use a separate array for negatives.
D. The algorithm doesn’t support negatives.
Answer: A
Explanation: To handle negative numbers, you can shift the range (e.g., add the absolute value of the minimum) to make all values non-negative before applying Counting Sort.
Question 14:
What is the purpose of the cumulative sum in Counting Sort?
A. To count the total elements.
B. To determine the correct positions for elements in the output.
C. To sort the counting array.
D. To remove duplicates.
Answer: B
Explanation: The cumulative sum adjusts the counting array to reflect the final positions, allowing elements to be placed in sorted order.
Question 15:
Counting Sort is a:
A. Comparison-based sort.
B. Non-comparison-based sort.
C. Hybrid sort.
D. Recursive sort.
Answer: B
Explanation: Counting Sort does not compare elements; it uses arithmetic operations based on keys, classifying it as a non-comparison-based sort.
Question 16:
For an array of 10 elements with values from 1 to 5, how many passes does Counting Sort typically make?
A. 1 pass
B. 2 passes
C. 3 passes
D. Variable passes
Answer: C
Explanation: Counting Sort generally involves three main passes: one to count frequencies, one to compute cumulative sums, and one to build the output array.
Question 17:
If the input array is [4, 2, 2, 8, 3, 3, 1], what will be the first element in the sorted output using Counting Sort?
A. 1
B. 2
C. 3
D. 4
Answer: A
Explanation: After processing, the smallest element, 1, will be placed first in the output array based on the counting and cumulative arrays.
Question 18:
Counting Sort can be unstable if:
A. Elements are not integers.
B. The implementation does not preserve order of equal elements.
C. The array has duplicates.
D. It is always stable.
Answer: B
Explanation: Counting Sort can be implemented to be stable by processing elements in reverse order when placing them in the output array, but a poor implementation might make it unstable.
Question 19:
Compared to Bubble Sort, Counting Sort is better for:
A. Nearly sorted arrays.
B. Arrays with a small range of values.
C. Random arrays of large size.
D. Arrays with floating-point numbers.
Answer: B
Explanation: Bubble Sort is O(n^2), while Counting Sort is O(n + k) for small k, making it superior for arrays with limited distinct values.
Question 20:
In Counting Sort, the final output array is:
A. The same as the input array.
B. A new array with elements in sorted order.
C. Reversed from the input.
D. Partially sorted.
Answer: B
Explanation: The algorithm produces a completely sorted array as output, based on the frequencies and positions derived from the counting array.
or
Part 3: AI Question Generator – Automatically Create Questions for Your Next Assessment
Automatically generate questions using AI