Counting Sort in C++
Counting Sort Algorithm in C++ (Complete Implementation).
1#include <iostream>2#include <algorithm>3using namespace std;45void countingSort(int arr[], int n) {6 int max = *max_element(arr, arr + n);7 int min = *min_element(arr, arr + n);8 int range = max - min + 1;910 int count[range] = {0};11 int output[n];1213 // Store count of each element14 for (int i = 0; i < n; i++)15 count[arr[i] - min]++;1617 // Change count to position18 for (int i = 1; i < range; i++)19 count[i] += count[i - 1];2021 // Build output array22 for (int i = n - 1; i >= 0; i--) {23 output[count[arr[i] - min] - 1] = arr[i];24 count[arr[i] - min]--;25 }2627 // Copy output to original array28 for (int i = 0; i < n; i++)29 arr[i] = output[i];30}3132void printArray(int arr[], int n) {33 for (int i = 0; i < n; i++) {34 cout << arr[i] << " ";35 }36 cout << endl;37}3839int main() {40 int arr[] = {4, 2, 2, 8, 3, 3, 1};41 int n = sizeof(arr) / sizeof(arr[0]);4243 cout << "Original array: ";44 printArray(arr, n);4546 countingSort(arr, n);4748 cout << "Sorted array: ";49 printArray(arr, n);5051 return 0;52}
Output
Original array: 4 2 2 8 3 3 1 Sorted array: 1 2 2 3 3 4 8
What's going on
The program sorts an array of integers using the Counting Sort algorithm. For example:
Counting Sort works by counting how many times each value appears, then placing each element in its correct position based on these counts.
Header Files Used
Understanding Counting Sort
Algorithm Concept
:
Visual Example
:
Array: [4, 2, 2, 8, 3, 3, 1]
Step 1: Count occurrences
Step 2: Calculate positions (cumulative counts)
Step 3: Place elements
Function: countingSort()
void countingSort(int arr[], int n) {
int max = *max_element(arr, arr + n);
int min = *min_element(arr, arr + n);
int range = max - min + 1;
int count[range] = {0};
int output[n];
for (int i = 0; i < n; i++)
count[arr[i] - min]++;
// Change count to position
for (int i = 1; i < range; i++)
count[i] += count[i - 1];
// Build output array
for (int i = n - 1; i >= 0; i--) {
output[count[arr[i] - min] - 1] = arr[i];
count[arr[i] - min]--;
}
// Copy output to original array
for (int i = 0; i < n; i++)
arr[i] = output[i];
}
1// Store count of each element
How it works
:
Step-by-Step Algorithm
Step 1: Find Range
Step 2: Count Occurrences
Step 3: Calculate Positions
Step 4: Build Output
Understanding Count to Position Conversion
Before
(counts):
count[0] = 1 (value 1 appears 1 time)
count[1] = 2 (value 2 appears 2 times)
count[2] = 2 (value 3 appears 2 times)
count[3] = 1 (value 4 appears 1 time)
count[7] = 1 (value 8 appears 1 time)
After
(positions):
count[0] = 1 (value 1 at position 0)
count[1] = 3 (value 2 at positions 1-2)
count[2] = 5 (value 3 at positions 3-4)
count[3] = 6 (value 4 at position 5)
count[7] = 7 (value 8 at position 6)
Time and Space Complexity
Time Complexity
: O(n + k)
Space Complexity
: O(n + k)
Stability
: Stable
When to Use Counting Sort
Best For
:
Not Recommended For
:
Important Considerations
Range Calculation
:
Stability
:
Index Mapping
:
return 0;
This ends the program successfully.