Bird
Raised Fist0
DSA Cprogramming~30 mins

Counting Sort Algorithm in DSA C - Build from Scratch

Choose your learning style10 modes available
Counting Sort Algorithm
📖 Scenario: Imagine you are organizing a small library of books by their publication year. You want to sort the years quickly and efficiently using a simple method called Counting Sort.
🎯 Goal: Build a program that sorts an array of publication years using the Counting Sort algorithm.
📋 What You'll Learn
Create an array of publication years with exact values
Create a count array to store frequencies
Implement the counting sort logic
Print the sorted array
💡 Why This Matters
🌍 Real World
Counting sort is useful when sorting items with a limited range of integer keys, like sorting ages, grades, or years quickly.
💼 Career
Understanding counting sort helps in jobs involving data processing, optimization, and algorithm design where efficient sorting is needed.
Progress0 / 4 steps
1
Create the array of publication years
Create an integer array called years with these exact values: 2001, 1999, 2003, 1999, 2001, 2002 and an integer variable n set to 6.
DSA C
Hint

Use int years[] = {2001, 1999, 2003, 1999, 2001, 2002}; to create the array and int n = 6; for its size.

2
Create the count array and find max year
Create an integer variable max_year and set it to 2003. Then create an integer array called count of size max_year - 1999 + 1 initialized to zero.
DSA C
Hint

Use a loop to set all elements of count to zero. The size is max_year - 1999 + 1 because 1999 is the smallest year.

3
Implement the counting sort logic
Use a for loop with variable i from 0 to n - 1 to count the frequency of each year in years by incrementing count[years[i] - 1999]. Then modify count to hold cumulative sums using a for loop with variable i from 1 to max_year - 1999. Finally, create an integer array sorted of size n and fill it by placing each year from years at the correct position using the count array, decreasing the count after placing.
DSA C
Hint

First count how many times each year appears. Then change counts to positions. Finally, place each year in the sorted array from the end to keep order.

4
Print the sorted array
Use a for loop with variable i from 0 to n - 1 to print each element of the sorted array separated by a space.
DSA C
Hint

Use printf("%d ", sorted[i]); inside a loop to print all sorted years separated by spaces.

Practice

(1/5)
1. What is the main idea behind the Counting Sort algorithm?
easy
A. Counting the number of occurrences of each element to sort
B. Comparing each element with every other element
C. Dividing the array into halves and sorting recursively
D. Swapping adjacent elements repeatedly

Solution

  1. Step 1: Understand Counting Sort principle

    Counting Sort counts how many times each value appears in the input.
  2. Step 2: Use counts to place elements in order

    Using these counts, it places elements directly into the sorted output.
  3. Final Answer:

    Counting the number of occurrences of each element to sort -> Option A
  4. Quick Check:

    Counting occurrences = A [OK]
Hint: Counting occurrences helps sort without comparisons [OK]
Common Mistakes:
  • Thinking it compares elements like bubble sort
  • Confusing with divide and conquer methods
  • Assuming it swaps elements repeatedly
2. Which of the following is the correct way to declare the count array in C for counting sort if the maximum value in input is max_val?
easy
A. int count[max_val + 1];
B. int count[max_val];
C. int count[max_val - 1];
D. int count[max_val * 2];

Solution

  1. Step 1: Understand array size for counts

    The count array must hold counts for all values from 0 to max_val inclusive, so size is max_val + 1.
  2. Step 2: Choose correct declaration

    Declaring int count[max_val + 1]; ensures space for all possible values.
  3. Final Answer:

    int count[max_val + 1]; -> Option A
  4. Quick Check:

    Count array size = max_val + 1 [OK]
Hint: Count array size must cover all values including max_val [OK]
Common Mistakes:
  • Using max_val size misses last element count
  • Using max_val - 1 causes out-of-range errors
  • Using double size wastes memory unnecessarily
3. What will be the output of the counting sort on the array [4, 2, 2, 8, 3, 3, 1]?
medium
A. [1, 3, 2, 4, 8, 3, 2]
B. [1, 2, 2, 3, 3, 4, 8]
C. [8, 4, 3, 3, 2, 2, 1]
D. [2, 2, 3, 3, 4, 8, 1]

Solution

  1. Step 1: Count occurrences of each element

    1 appears once, 2 appears twice, 3 appears twice, 4 once, 8 once.
  2. Step 2: Place elements in sorted order using counts

    Sorted array is [1, 2, 2, 3, 3, 4, 8].
  3. Final Answer:

    [1, 2, 2, 3, 3, 4, 8] -> Option B
  4. Quick Check:

    Sorted array = C [OK]
Hint: Count then output in order for sorted array [OK]
Common Mistakes:
  • Confusing input order with output order
  • Forgetting to repeat elements by their counts
  • Mixing up counts and positions
4. Given the following code snippet for counting sort, what is the error?
int count[5] = {0};
int arr[6] = {1, 3, 4, 2, 3, 0};
for (int i = 0; i < 6; i++) {
    count[arr[i]]++;
}
for (int i = 0; i <= 5; i++) {
    printf("%d ", count[i]);
}
medium
A. Incrementing count[arr[i]] is wrong
B. Count array not initialized to zero
C. Array arr size is incorrect
D. Loop accessing count array goes out of bounds

Solution

  1. Step 1: Check count array size and loop range

    Count array size is 5 (indices 0 to 4), but loop runs i from 0 to 5 inclusive, accessing count[5] which is out of bounds.
  2. Step 2: Identify out-of-bounds access

    Accessing count[5] causes undefined behavior or error.
  3. Final Answer:

    Loop accessing count array goes out of bounds -> Option D
  4. Quick Check:

    Loop index > count size = D [OK]
Hint: Check array size vs loop limits carefully [OK]
Common Mistakes:
  • Ignoring array size when looping
  • Assuming initialization fixes out-of-bounds
  • Confusing arr size with count size
5. You want to sort an array containing negative and positive integers using counting sort. Which modification is necessary?
hard
A. Use the array as is because counting sort handles negatives naturally
B. Sort only positive numbers and ignore negatives
C. Shift all numbers by adding the absolute value of the minimum element before sorting
D. Use bubble sort instead

Solution

  1. Step 1: Understand counting sort limitation

    Counting sort works only with non-negative integers because it uses array indices as counts.
  2. Step 2: Adjust negative values for indexing

    Shift all numbers by adding the absolute value of the minimum element to make all values non-negative before counting.
  3. Final Answer:

    Shift all numbers by adding the absolute value of the minimum element before sorting -> Option C
  4. Quick Check:

    Shift negatives to non-negative = B [OK]
Hint: Shift negatives to zero or above before counting [OK]
Common Mistakes:
  • Trying to count negatives directly
  • Ignoring negative values in input
  • Switching to other sorts unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard