Counting sort is a sorting algorithm that works on integer keys within a specific range. It works by counting the number of objects with each distinct key value and using arithmetic to determine the position of each object in the output sequence without comparing elements. The algorithm uses three arrays: an input array, an output array, and a counting array. It iterates through the input array to count the frequency of each key value in the counting array. Then iterates through the counting array to calculate cumulative frequencies, and finally iterates through the input array again to place each object in the output array based on its key value's counted position. Counting sort runs in O(n+k) time where n is the number of elements and k is the maximum key