📖 Scenario: You are organizing a small library's book shelf by the number of pages each book has. You want to sort the books quickly based on their page counts, which are all between 0 and 9 pages for this example.
🎯 Goal: Build a program that uses the Counting Sort Algorithm to sort a list of book page counts in ascending order.
📋 What You'll Learn
Create an integer array called pages with the exact values: 4, 2, 2, 8, 3, 3, 1
Create an integer variable called maxPage and set it to 8
Implement the counting sort logic using arrays count and output
Print the sorted array output as space-separated values
💡 Why This Matters
🌍 Real World
Counting sort is useful when sorting items with a small range of integer keys quickly, like sorting exam scores or ages.
💼 Career
Understanding counting sort helps in roles involving data processing and optimization, such as software development and data analysis.
Progress0 / 4 steps
1
Create the initial array of book page counts
Create an integer array called pages with these exact values: 4, 2, 2, 8, 3, 3, 1
DSA C#
Hint
Use int[] pages = {4, 2, 2, 8, 3, 3, 1}; to create the array.
2
Set the maximum page count value
Create an integer variable called maxPage and set it to 8
DSA C#
Hint
Set maxPage to the highest number in the pages array.
3
Implement the counting sort logic
Create an integer array called count of size maxPage + 1 and initialize it with zeros. Create an integer array called output of the same size as pages. Use a for loop with variable i to count the occurrences of each page number in pages and store in count. Then, update count to hold cumulative counts using a for loop with variable i. Next, use a for loop with variable i going backwards over pages to place elements in output in sorted order.
DSA C#
Hint
Follow the counting sort steps: count frequencies, compute cumulative counts, then place elements in output array.
4
Print the sorted array
Use a for loop with variable i to print each element of the output array separated by spaces on one line.
DSA C#
Hint
Use a for loop to print each element of output followed by a space.
Practice
(1/5)
1. What is the main idea behind the Counting Sort Algorithm?
easy
A. Count the occurrences of each element and use counts to place elements in order
B. Compare each element with every other element to sort
C. Divide the array into halves and sort recursively
D. Swap adjacent elements if they are in wrong order repeatedly
Solution
Step 1: Understand Counting Sort principle
Counting sort counts how many times each value appears in the input.
Step 2: Use counts to place elements
These counts help to directly place elements in the sorted output without comparisons.
Final Answer:
Count the occurrences of each element and use counts to place elements in order -> Option A
Quick Check:
Counting occurrences = A [OK]
Hint: Counting sort uses frequency counts, not comparisons [OK]
Common Mistakes:
Thinking counting sort compares elements
Confusing counting sort with merge sort
Assuming counting sort works on all data types
2. Which of the following is the correct way to declare the count array for counting sort in C# if the input range is from 0 to 9?
easy
A. int[] count = new int[0];
B. int[] count = new int[10];
C. int[] count = new int[11];
D. int count = new int[9];
Solution
Step 1: Determine size of count array
For range 0 to 9, we need 10 slots to count each number.
Step 2: Correct C# syntax for array declaration
Use int[] count = new int[10]; to create an array of size 10.
Final Answer:
int[] count = new int[10]; -> Option B
Quick Check:
Array size = max value + 1 = 10 [OK]
Hint: Count array size = max element value + 1 [OK]
Common Mistakes:
Using size less than max value + 1
Declaring count as single int instead of array
Using incorrect array size causing index errors
3. What will be the output of the following C# code snippet using counting sort on array int[] arr = {4, 2, 2, 8, 3, 3, 1}?
int max = 8;
int[] count = new int[max + 1];
int[] output = new int[arr.Length];
// Count occurrences
foreach (int num in arr) count[num]++;
// Modify count array
for (int i = 1; i <= max; i++) count[i] += count[i - 1];
// Build output
for (int i = arr.Length - 1; i >= 0; i--) {
output[count[arr[i]] - 1] = arr[i];
count[arr[i]]--;
}
// Print output
foreach (int num in output) Console.Write(num + " ");
medium
A. 1 2 2 3 3 4 8
B. 8 4 3 3 2 2 1
C. 1 3 2 2 3 4 8
D. 2 2 3 3 4 8 1
Solution
Step 1: Count occurrences of each number
Count array after counting: [0,1,2,2,1,0,0,0,1]
Step 2: Modify count array to cumulative counts
Cumulative count: [0,1,3,5,6,6,6,6,7]
Step 3: Place elements in output using counts
Placing elements from right to left gives sorted array: 1 2 2 3 3 4 8
Final Answer:
1 2 2 3 3 4 8 -> Option A
Quick Check:
Sorted array = 1 2 2 3 3 4 8 [OK]
Hint: Use cumulative counts to place elements from right [OK]
Common Mistakes:
Not using cumulative counts before placing elements
Placing elements from left to right causing wrong order
Off-by-one errors in indexing output
4. Identify the error in this counting sort code snippet and choose the correct fix:
int max = 5;
int[] count = new int[max];
int[] arr = {1, 4, 3, 2, 5};
foreach (int num in arr) {
count[num]++;
}
medium
A. Initialize count array with zeros explicitly
B. Change foreach loop to for loop
C. Change count array size to max + 1 to avoid index out of range
D. No error, code is correct
Solution
Step 1: Analyze count array size
Count array size is 5, but max value is 5, so index 5 is out of range.
Step 2: Fix by increasing count array size
Count array should be size max + 1 = 6 to include index 5 safely.
Final Answer:
Change count array size to max + 1 to avoid index out of range -> Option C
Quick Check:
Array size = max + 1 to cover all indices [OK]
Hint: Count array size must be max element + 1 [OK]
Common Mistakes:
Using count array size equal to max value only
Ignoring index out of range exceptions
Assuming default array size is enough
5. You have an array int[] arr = {5, 3, 0, 2, 3, 5, 1, 4} and want to sort it using counting sort. However, the array may contain negative numbers in future. How should you modify the counting sort algorithm to handle negative numbers correctly?
hard
A. Sort negative and positive numbers separately and then merge
B. Ignore negative numbers as counting sort only works for positive numbers
C. Use a dictionary instead of an array for counts
D. Shift all numbers by adding the absolute value of the minimum negative number before counting
Solution
Step 1: Identify problem with negative numbers
Counting sort uses array indices as counts, negative indices are invalid.
Step 2: Shift numbers to non-negative range
Add absolute value of minimum negative number to all elements to make them non-negative.
Step 3: Apply counting sort on shifted values
Sort shifted values, then shift back to original values after sorting.
Final Answer:
Shift all numbers by adding the absolute value of the minimum negative number before counting -> Option D
Quick Check:
Shift negatives to zero or above before counting [OK]
Hint: Shift negatives to zero or above before counting [OK]
Common Mistakes:
Trying to use negative indices in count array
Ignoring negative values in input
Using dictionary without sorting logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.