Skip to main content

Counting Sort


Counting sort is a sorting algorithm that operates by counting the frequency of each element in the input array, and then using these frequencies to compute the final sorted output.

The algorithm works by first creating an array of counters, with each counter corresponding to a distinct element in the input array. The counters are initialized to zero, and then the input array is scanned once to count the number of occurrences of each element. This count information is stored in the corresponding counter in the counter array.

Once the count information is obtained, the algorithm uses it to determine the final position of each element in the sorted output array. This is done by iterating over the counter array, and for each counter value, writing the corresponding element to the sorted output array the appropriate number of times.

The time complexity of counting sort is O(n+k), where n is the length of the input array and k is the range of the elements. The space complexity is also O(n+k), since the algorithm requires a counter array of size k.

Counting sort is an efficient sorting algorithm when the range of elements is small compared to the length of the input array. In such cases, counting sort can outperform comparison-based sorting algorithms such as quicksort and mergesort, which have a worst-case time complexity of O(n log n). However, counting sort has limitations in practice, since it requires that the range of elements be known in advance, and it can also use a lot of memory for large ranges of elements.

Despite its limitations, counting sort is a useful algorithm for sorting integers with a small range of values, such as in applications involving histograms, character counts, and scoreboards. It is also used as a subroutine in other algorithms, such as radix sort and bucket sort, which extend its applicability to more general sorting problems.

ALGORITHM:

The algorithm for counting sort can be summarized in the following steps:
  1. Find the maximum value in the input array, and use this value to determine the size of the counter array.
  2. Initialize the counter array to all zeros.
  3. Iterate over the input array, and use the values as indices into the counter array. Increment the counter at each index for each occurrence of the corresponding value in the input array.
  4. Modify the counter array so that each element contains the sum of the previous elements. This step is what gives counting sort its name, since it is effectively a "counting" and "summing" process.
  5. Create a new output array of the same size as the input array.
  6. Iterate over the input array in reverse order, using the values as indices into the counter array to determine the correct position in the output array for each element.
  7. Decrement the counter at each index after placing the corresponding element in the output array.
  8. The output array now contains the sorted elements.

PSEUDO CODE:

Here is the pseudocode for counting sort algorithm:


The countingSort function takes an input array array and the maximum value of elements max_val. It first creates a counter array counter of size max_val + 1, with all elements initialized to 0. Then, it loops over the array and increments the corresponding counter in the counter array for each occurrence of a value in the array.

Next, it modifies the counter array to make it contain the cumulative sum of the previous elements in the array. This step is necessary to compute the correct position of each element in the output array.

Then, the function creates a new output array of the same size as the input array. It loops over the input array, and for each element, it uses the counter array to determine the correct position of the element in the output array. It then decrements the corresponding counter value.

Finally, the function returns the sorted output array.

PYTHON CODE:

Here is the python code for counting sort algorithm:


This implementation takes an input array arr and returns the sorted array using counting sort algorithm.

In Step 1, it finds the maximum value in the input array arr. 

In Step 2, it creates a counter array counter of size max_val + 1, with all elements initialized to 0. 

In Step 3, it loops over the arr and increments the corresponding counter in the counter array for each occurrence of a value in the arr.

In Step 4, it modifies the counter array to make it contain the cumulative sum of the previous elements in the array. This step is necessary to compute the correct position of each element in the output array.

In Step 5, it creates a new output array of the same size as the input array. 

In Step 6, it loops over the input array, and for each element, it uses the counter array to determine the correct position of the element in the output array. It then decrements the corresponding counter value.

Finally, it returns the sorted output array in Step 7.

JAVA CODE:

Here is the Java code for counting sort algorithm:



This implementation takes an input array arr and returns the sorted array using counting sort algorithm.

In Step 1, it finds the maximum value in the input array arr. 

In Step 2, it creates a counter array counter of size maxVal + 1, with all elements initialized to 0. 

In Step 3, it loops over the arr and increments the corresponding counter in the counter array for each occurrence of a value in the arr.

In Step 4, it modifies the counter array to make it contain the cumulative sum of the previous elements in the array. This step is necessary to compute the correct position of each element in the output array.

In Step 5, it creates a new output array of the same size as the input array. 

In Step 6, it loops over the input array, and for each element, it uses the counter array to determine the correct position of the element in the output array. It then decrements the corresponding counter value.

Finally, it returns the sorted output array in Step 7.

C CODE:

Here is the C code for counting sort algorithm:


This implementation takes an input array arr and its length n, and sorts the array in place using the counting sort algorithm.

In the counting_sort function, it first finds the maximum value in the input array arr. It then creates a counter array counter of size max_val + 1, with all elements initialized to 0. It loops over the arr and increments the corresponding counter in the counter array for each occurrence of a value in the arr.

It then modifies the counter array to make it contain the cumulative sum of the previous elements in the array. This step is necessary to compute the correct position of each element in the output array.

It creates a new output array of the same size as the input array. It loops over the input array in reverse order, and for each element, it uses the counter array to determine the correct position of the element in the output array. It then decrements the corresponding counter value.

Finally, it copies the sorted output array back to the input array arr, and frees the memory used by the counter and output arrays.

In the main function, it creates an example input array, calls the counting_sort function to sort it, and prints the sorted array.

C++ CODE:

Here is the C++ code for counting sort algorithm:


This implementation takes an input vector arr and sorts the vector in place using the counting sort algorithm.

In the countingSort function, it first finds the maximum value in the input vector arr. It then creates a counter vector counter of size max_val + 1, with all elements initialized to 0. It loops over the arr and increments the corresponding counter in the counter vector for each occurrence of a value in the arr.

It then modifies the counter vector to make it contain the cumulative sum of the previous elements in the vector. This step is necessary to compute the correct position of each element in the output vector.

It creates a new output vector of the same size as the input vector. It loops over the input vector in reverse order, and for each element, it uses the counter vector to determine the correct position of the element in the output vector. It then decrements the corresponding counter value.

Finally, it assigns the sorted output vector back to the input vector arr.

In the main function, it creates an example input vector, calls the countingSort function to sort it, and prints the sorted vector.

JAVASCRIPT CODE:

Here is the Javascript code for counting sort algorithm:


This implementation takes an input array arr and sorts the array in place using the counting sort algorithm.

In the countingSort function, it first finds the maximum value in the input array arr. It then creates a count array countArr of size maxVal + 1, with all elements initialized to 0. It loops over the arr and increments the corresponding count in the countArr array for each occurrence of a value in the arr.

It then modifies the countArr array to make it contain the cumulative sum of the previous elements in the array. This step is necessary to compute the correct position of each element in the output array.

It creates a new output array of the same size as the input array. It loops over the input array in reverse order, and for each element, it uses the count array to determine the correct position of the element in the output array. It then decrements the corresponding count value.

Finally, it assigns the sorted output array back to the input array arr.

In the example usage, it creates an example input array, calls the countingSort function to sort it, and prints the sorted array to the console.

Comments

Post a Comment

Popular posts from this blog

Selection Sort

Selection sort is a simple sorting algorithm that sorts an array by repeatedly finding the minimum element from the unsorted portion of the array and swapping it with the first element of the unsorted portion. It is called selection sort because it repeatedly selects the smallest element of the unsorted portion of the array and moves it to its correct position in the sorted portion of the array. The time complexity of selection sort is O(n^2), where n is the number of elements in the array. This makes it inefficient for large arrays and it is not a good choice for sorting large datasets. However, selection sort is easy to understand and implement, and it is useful for educational purposes and for small arrays. One of the advantages of selection sort is that it makes fewer swaps than other sorting algorithms such as bubble sort. This makes it more efficient in terms of memory writes, which can be important in some situations. Another advantage of selection sort is that it is a stable so

Pigeonhole Sort

Pigeonhole sort is a sorting algorithm that works by distributing elements of an input sequence into a set of pigeonholes or buckets. It is an efficient sorting algorithm for small ranges of values where the number of elements to be sorted is roughly the same as the range of values. Pigeonhole sort is an example of a bucket sort algorithm. The pigeonhole sort algorithm works by first determining the range of values in the input sequence. This range is used to create a set of pigeonholes or buckets, each of which can hold one or more elements from the input sequence. Each element in the input sequence is then placed into its corresponding pigeonhole based on its value. If two or more elements have the same value, they can be placed in the same pigeonhole. Once all the elements have been placed into their corresponding pigeonholes, the elements are sorted within each pigeonhole. Finally, the sorted elements are combined into a single output sequence. Pigeonhole sort has a time complexity