- What is Counting Sort and when to use it
- How to implement Counting Sort in C programming
- How to handle negative numbers and large ranges
- Time and space complexity analysis
- When Counting Sort is better than comparison-based sorts
Introduction to Counting Sort
Counting Sort is a non-comparison-based sorting algorithm that sorts integers by counting the occurrences of each unique element. Unlike comparison-based algorithms (like Quick Sort or Merge Sort), Counting Sort uses the actual values of the elements as indices into an array.
Key characteristics of Counting Sort:
- Linear time complexity: O(n + k) where k is the range of input
- Stable: Preserves the relative order of equal elements
- Not in-place: Requires extra memory for counting and output arrays
- Efficient for small integer ranges: Works best when the range of values is not too large
💡 When to use: Counting Sort is ideal when you know the range of values is small (e.g., test scores from 0-100, ages, or grades). It's perfect for sorting integers with a limited range.
C Program for Counting Sort
#include <stdio.h>
#include <stdlib.h>
// Function to find the maximum element in an array
int findMax(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
// Function to find the minimum element in an array
int findMin(int arr[], int n) {
int min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < min) {
min = arr[i];
}
}
return min;
}
// Counting Sort function
void countingSort(int arr[], int n) {
// Find the range of values
int min = findMin(arr, n);
int max = findMax(arr, n);
int range = max - min + 1;
// Create count array and output array
int *count = (int *)calloc(range, sizeof(int));
int *output = (int *)malloc(n * sizeof(int));
// Count occurrences of each element
for (int i = 0; i < n; i++) {
count[arr[i] - min]++;
}
// Modify count array to store cumulative counts
for (int i = 1; i < range; i++) {
count[i] += count[i - 1];
}
// Build the output array
for (int i = n - 1; i >= 0; i--) {
int index = arr[i] - min;
output[count[index] - 1] = arr[i];
count[index]--;
}
// Copy output array back to original array
for (int i = 0; i < n; i++) {
arr[i] = output[i];
}
// Free allocated memory
free(count);
free(output);
}
// Function to print the array
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
// Driver program to test the counting sort
int main() {
int arr[] = {4, 2, 2, 8, 3, 3, 1, 5, 7, 6, 9, 2, 4, 6, 8};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);
countingSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
Sample Output
Original array: 4 2 2 8 3 3 1 5 7 6 9 2 4 6 8 Sorted array: 1 2 2 2 3 3 4 4 5 6 6 7 8 8 9
Example with negative numbers:
Original array: -5 -2 8 0 3 -1 4 7 -3 6 Sorted array: -5 -3 -2 -1 0 3 4 6 7 8
Program Explanation
Let's break down the code step by step:
- Include Header Files:
#include <stdio.h>and#include <stdlib.h>for input/output and memory allocation. - Find Min and Max:
findMax()andfindMin()determine the range of values- This allows the algorithm to handle negative numbers and any integer range
- Counting Sort Function:
- Step 1: Find range = max - min + 1
- Step 2: Create a count array of size 'range' initialized to 0
- Step 3: Count occurrences:
count[arr[i] - min]++ - Step 4: Cumulative count:
count[i] += count[i-1] - Step 5: Build output array by placing elements in sorted order
- Step 6: Copy output back to original array
- Memory Management: Uses
calloc()andmalloc()for dynamic memory allocation, andfree()to prevent memory leaks. - PrintArray Function: Displays all elements of the array.
- Main Function:
- Creates an unsorted array
- Displays the original array
- Calls
countingSort()to sort - Displays the sorted array
📝 Note: Counting Sort is stable, meaning equal elements maintain their relative order. This is important when sorting data with multiple keys.
Step-by-Step Approach
| Step | Action | Explanation |
|---|---|---|
| 1 | Find Range | Determine min and max values |
| 2 | Create Count Array | Allocate array of size 'range' |
| 3 | Count Occurrences | Count frequency of each element |
| 4 | Cumulative Count | Calculate positions in sorted array |
| 5 | Build Output | Place elements in sorted order |
| 6 | Copy Back | Copy sorted elements to original array |
Algorithm for Counting Sort
- Find Range:
min = findMin(arr, n)max = findMax(arr, n)range = max - min + 1
- Count Occurrences:
- For each element
arr[i], incrementcount[arr[i] - min]
- For each element
- Cumulative Count:
- For
i = 1torange-1:count[i] += count[i-1]
- For
- Build Output:
- For
i = n-1down to0: index = arr[i] - minoutput[count[index] - 1] = arr[i]count[index]--
- For
- Copy Back: Copy output array to original array
Time & Space Complexity
| Complexity | Best Case | Average Case | Worst Case |
|---|---|---|---|
| Time | O(n + k) | O(n + k) | O(n + k) |
| Space | O(k) - Extra space for count array | ||
📊 Key Points:
- Linear time: Counting Sort runs in O(n + k) time, where k is the range of input
- Space trade-off: Extra memory O(k) is needed for the count array
- Stable: Preserves relative order of equal elements
- Best for: Small integer ranges with high performance requirements
💻 Practice Exercise
Challenge 1: Modify the program to sort an array of characters (e.g., 'a', 'b', 'c') using Counting Sort.
Challenge 2: Write a program to sort an array of student grades (0-100) using Counting Sort.
Challenge 3: Implement Counting Sort for an array where the range is very large (e.g., 0 to 1,000,000) and optimize it.
🔍 Click to Show Solution for Challenge 1
// For characters, convert to ASCII values
void countingSortChar(char arr[], int n) {
// ASCII range: 0-255
int range = 256;
int *count = (int *)calloc(range, sizeof(int));
char *output = (char *)malloc(n * sizeof(char));
for (int i = 0; i < n; i++) {
count[(int)arr[i]]++;
}
for (int i = 1; i < range; i++) {
count[i] += count[i-1];
}
for (int i = n-1; i >= 0; i--) {
int idx = (int)arr[i];
output[count[idx] - 1] = arr[i];
count[idx]--;
}
for (int i = 0; i < n; i++) {
arr[i] = output[i];
}
free(count);
free(output);
}
🔍 Click to Show Solution for Challenge 2
// Grade sorting (0-100 range)
void sortGrades(int grades[], int n) {
// Range: 0 to 100
const int MAX_GRADE = 100;
int count[MAX_GRADE + 1] = {0};
int output[n];
for (int i = 0; i < n; i++) {
count[grades[i]]++;
}
for (int i = 1; i <= MAX_GRADE; i++) {
count[i] += count[i-1];
}
for (int i = n-1; i >= 0; i--) {
output[count[grades[i]] - 1] = grades[i];
count[grades[i]]--;
}
for (int i = 0; i < n; i++) {
grades[i] = output[i];
}
}
Frequently Asked Questions
1. When should I use Counting Sort?
Counting Sort is best when the range of possible values (k) is not significantly larger than the number of elements (n). It's ideal for sorting integers with a small, known range like exam scores, ages, or ASCII characters.
2. Why is Counting Sort not suitable for large ranges?
Counting Sort requires O(k) extra space for the count array. If the range is huge (e.g., 0 to 1,000,000,000), the memory usage becomes impractical, making other algorithms more suitable.
3. Is Counting Sort faster than Quick Sort?
For small integer ranges, yes! Counting Sort runs in O(n + k) time, which is linear. Quick Sort has O(n log n) average complexity. However, for large ranges, Quick Sort may be more practical due to space constraints.
4. Can Counting Sort handle negative numbers?
Yes! By finding the minimum value and using it as an offset, we can map negative numbers to positive indices in the count array. The code above implements this using the arr[i] - min technique.
5. Is Counting Sort stable?
Yes, Counting Sort is stable. It preserves the relative order of equal elements because we iterate from the end of the array when building the output, which maintains the original order of duplicate elements.
💡 Pro Tip: Counting Sort is an excellent choice for sorting data with a limited range, such as sorting test scores, student grades, or counting frequencies in data analysis.