Count Sort in c++

Count Sort in c++

 Count Sort is an efficient and stable sorting algorithm that is based on counting the number of occurrences of each element in the input array. The algorithm then uses this information to sort the array in a linear time complexity, making it an efficient choice for sorting large datasets. In this article, we will explore the Count Sort algorithm in detail and implement it in C++.

The Count Sort algorithm works by counting the number of occurrences of each element in the input array. Once this information is gathered, the algorithm creates a new array, called the output array, that will be used to store the sorted elements. The output array is initialized with zeroes, and the algorithm then iterates through the input array, incrementing the corresponding element in the output array for each occurrence of that element.

Once the counting is complete, the algorithm iterates through the output array, creating a new array that contains the sorted elements. This is done by iterating through the output array, adding the elements to the new array in the order they appear in the output array.

One of the key advantages of Count Sort is its linear time complexity, making it an efficient choice for sorting large datasets. This is because the algorithm does not need to compare elements in the input array, which can take a significant amount of time when sorting large datasets. Additionally, Count Sort is a stable sorting algorithm, which means that elements that have the same value will maintain their relative order in the output array.

To implement Count Sort in C++, we will first need to create a function that takes in the input array and its size as arguments. The function will then initialize the output array with zeroes and iterate through the input array, counting the number of occurrences of each element. Once this information is gathered, the function will iterate through the output array, adding the elements to the new array in the order they appear in the output array.

Here is an example implementation of Count Sort in C++:

#include <iostream>
#include <vector>

void countSort(std::vector<int>& arr) {
    // Find the maximum element in the input array
    int max = *std::max_element(arr.begin(), arr.end());
    // Initialize the output array with zeroes
    std::vector<int> output(max + 1, 0);
    // Count the number of occurrences of each element in the input array
    for (auto& i : arr) {
        output[i]++;
    }
    // Iterate through the output array, adding the elements to the new array in the order they appear
    int j = 0;
    for (int i = 0; i <= max; i++) {
        while (output[i]--) {
            arr[j++] = i;
        }
    }
}

int main() {
    std::vector<int> arr {5, 3, 2, 8, 1, 4};
    countSort(arr);
    for (auto& i : arr) {
        std::cout << i << " ";
    }
    return 0;
}

In this example, we first find the maximum element in the input array using the std::max_element function. We then initialize the output array with zeroes, using the std::vector class. The input array is then iterated through, counting the number of occurrences of each element. Once this information is gathered, the output array is iterated through, adding the elements to the new array in the