Data Structures and Algorithms Lab Manual
Experiment: Counting Sort Implementation
Objective:
To understand and implement the Counting Sort algorithm using C++ and analyze its working.
Theory:
Counting Sort is a non-comparison-based sorting algorithm that sorts integers by counting the
occurrences of each element. It works efficiently for integers or objects that can be mapped to a
range of integers. The algorithm assumes that the range of the input data is known beforehand.
Steps of Counting Sort:
1. Determine the range of the input (minimum and maximum values).
2. Create a count array to store the frequency of each element.
3. Update the count array to hold cumulative counts.
4. Build the sorted array by placing elements at their correct positions using the cumulative
count array.
5. Copy the sorted array back to the original array.
Advantages:
Time complexity is O(n + k), where n is the number of elements and k is the range of
input values.
Suitable for sorting datasets with small integer ranges.
Disadvantages:
Requires extra space for the count array.
Not efficient for large ranges.
Algorithm:
void countingSort(int arr[], int n) {
// Step 1: Find the maximum element
int max = *max_element(arr, arr + n);
int min = *min_element(arr, arr + n);
int range = max - min + 1;
// Step 2: Create count array and initialize to 0
vector<int> count(range, 0);
// Step 3: Store frequency of each element
for (int i = 0; i < n; i++) {
count[arr[i] - min]++;
}
// Step 4: Update count array to store cumulative counts
for (int i = 1; i < range; i++) {
count[i] += count[i - 1];
}
// Step 5: Build the sorted array
vector<int> output(n);
for (int i = n - 1; i >= 0; i--) {
output[--count[arr[i] - min]] = arr[i];
}
// Step 6: Copy the sorted array back to the original array
for (int i = 0; i < n; i++) {
arr[i] = output[i];
}
}
Program Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
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;
vector<int> count(range, 0);
for (int i = 0; i < n; i++) {
count[arr[i] - min]++;
}
for (int i = 1; i < range; i++) {
count[i] += count[i - 1];
}
vector<int> output(n);
for (int i = n - 1; i >= 0; i--) {
output[--count[arr[i] - min]] = arr[i];
}
for (int i = 0; i < n; i++) {
arr[i] = output[i];
}
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
countingSort(arr, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
Sample Input/Output:
Input:
Enter the number of elements: 6
Enter the elements: 4 2 2 8 3 3
Output:
Sorted array: 2 2 3 3 4 8
Observations:
Test Case Input Output
1 422833 223348
2 1412752 1122457
3 10 20 15 10 5 30 5 10 10 15 20 30
Viva Questions:
1. What is the time complexity of Counting Sort?
2. Can Counting Sort handle negative numbers? Why or why not?
3. How does Counting Sort differ from comparison-based sorting algorithms?
4. What is the space complexity of Counting Sort?
5. In which scenarios would Counting Sort not be a suitable choice?
Conclusion:
The Counting Sort algorithm is an efficient method for sorting data with a known range. It
avoids comparisons by using frequency counts, making it faster for certain datasets. However, it
is not ideal for datasets with large ranges due to high space requirements.