0% completed
Counting Sort is a non-comparative sorting algorithm that sorts integers by counting the occurrences of each unique element. It then uses this count to determine the position of each element in the sorted output array. This algorithm is particularly effective when the range of the input elements is not significantly larger than the number of elements.
Counting Sort is best suited for sorting integers or objects that can be mapped to integers, making it efficient with a time complexity of O(n + k), where n is the number of elements and k is the range of the input.
Counting Sort stands out because it is stable and works well with large datasets where the range of input values is manageable. It does not rely on comparisons, making it faster than comparison-based sorting algorithms in specific scenarios.
We will learn the counting sort by applying it to the array = [4, 2, 2, 8, 3, 3, 1]
.
max + 1
where max
is the maximum value found in the input array.countArray[array[i]] - 1
.Input: array1 = [4, 2, 2, 8, 3, 3, 1]
Find the Maximum Value in the Array
Create and Initialize the Count Array
8 + 1 = 9
: [0, 0, 0, 0, 0, 0, 0, 0, 0]
Count the Occurrences of Each Element
{4, 2, 2, 8, 3, 3, 1}
countArray[4]++
-> [0, 0, 0, 0, 1, 0, 0, 0, 0]
countArray[2]++
-> [0, 0, 1, 0, 1, 0, 0, 0, 0]
countArray[2]++
-> [0, 0, 2, 0, 1, 0, 0, 0, 0]
countArray[8]++
-> [0, 0, 2, 0, 1, 0, 0, 0, 1]
countArray[3]++
-> [0, 0, 2, 1, 1, 0, 0, 0, 1]
countArray[3]++
-> [0, 0, 2, 2, 1, 0, 0, 0, 1]
countArray[1]++
-> [0, 1, 2, 2, 1, 0, 0, 0, 1]
Modify the Count Array to Store Cumulative Counts
countArray[1] += countArray[0]
-> [0, 1, 2, 2, 1, 0, 0, 0, 1]
countArray[2] += countArray[1]
-> [0, 1, 3, 2, 1, 0, 0, 0, 1]
countArray[3] += countArray[2]
-> [0, 1, 3, 5, 1, 0, 0, 0, 1]
countArray[4] += countArray[3]
-> [0, 1, 3, 5, 6, 0, 0, 0, 1]
countArray[5] += countArray[4]
-> [0, 1, 3, 5, 6, 6, 0, 0, 1]
countArray[6] += countArray[5]
-> [0, 1, 3, 5, 6, 6, 6, 0, 1]
countArray[7] += countArray[6]
-> [0, 1, 3, 5, 6, 6, 6, 6, 1]
countArray[8] += countArray[7]
-> [0, 1, 3, 5, 6, 6, 6, 6, 7]
Build the Output Array
{4, 2, 2, 8, 3, 3, 1}
outputArray[countArray[1] - 1] = 1
-> [1, 0, 0, 0, 0, 0, 0]
; countArray[1]--
-> [0, 0, 3, 5, 6, 6, 6, 6, 7]
outputArray[countArray[3] - 1] = 3
-> [1, 0, 0, 0, 3, 0, 0]
; countArray[3]--
-> [0, 0, 3, 4, 6, 6, 6, 6, 7]
outputArray[countArray[3] - 1] = 3
-> [1, 0, 0, 3, 3, 0, 0]
; countArray[3]--
-> [0, 0, 3, 3, 6, 6, 6, 6, 7]
outputArray[countArray[8] - 1] = 8
-> [1, 0, 0, 3, 3, 0, 8]
; countArray[8]--
-> [0, 0, 3, 3, 6, 6, 6, 6, 6]
outputArray[countArray[2] - 1] = 2
-> [1, 0, 2, 3, 3, 0, 8]
; countArray[2]--
-> [0, 0, 2, 3, 6, 6, 6, 6, 6]
outputArray[countArray[2] - 1] = 2
-> [1, 2, 2, 3, 3, 0, 8]
; countArray[2]--
-> [0, 0, 1, 3, 6, 6, 6, 6, 6]
outputArray[countArray[4] - 1] = 4
-> [1, 2, 2, 3, 3, 4, 8]
; countArray[4]--
-> [0, 0, 1, 3, 5, 6, 6, 6, 6]
Copy the Output Array Back to the Input Array
array1 = {1, 2, 2, 3, 3, 4, 8}
Overall time complexity: O(n + k)
Overall space complexity: O(n + k)
.....
.....
.....