Medium Algorithms Sorting Algorithms
Explain counting sort and when is it most efficient?
Answer
Counting sort is a non-comparison sorting algorithm for integers in a known range [0,k]. Count occurrences of each value, compute cumulative counts (positions), place elements in output array using counts. Time complexity is O(n+k), space is O(n+k). Most efficient when k is O(n) - achieves linear time. Not suitable when range is much larger than n. Serves as subroutine in radix sort. It's stable sort.
IIT Certified
Master These Concepts with IIT Certification
175+ hours of industry projects. Get placed at Bosch, Tata Motors, L&T and 500+ companies.
Relevant for Roles
Software Engineer Algorithm Developer Systems Developer