

How does the counting sort algorithm works? Well, counting sort creates a bucket for each value and keeps a counter in each bucket. If you face this question in the future, you can mention Radix sort, Bucket sort, or Counting sort algorithms. It's quite common in Java interviews nowadays to ask, whether do you know any O(n) sorting algorithm or not. It is also one of the few linear sorting algorithms or O(n) sorting algorithms. Hence counting sort is among the fastest sorting algorithms around, in theory. the values of the input array are assumed to be integers), non-comparison, and linear sorting algorithm. Compare A with item.The Counting sort algorithm, like Radix sort and Bucket sort, is an integer-based algorithm (i.e.Repeat Step 4.1 and 4.2 till x item, skip to A (beginning of the current block) and perform a linear search.įigure 7: Comparing A and item (Linear Search).If A= item, then print x as the valid location else set x++ At any point in time, if ( x item, then move to the beginning of the current block and perform a linear search.Iteration 3: if ( x=A), then success, else, if ( x > A), then jump to the next block.Iteration 2: if ( x=A), then success, else, if ( x > A), then jump to the next block.Iteration 1: if ( x=A), then success, else, if ( x > A), then jump to the next block.

For implementing this algorithm, a block of size m is also required, that can be skipped or jumped in every iteration. Lets consider a sorted array A of size n, with indexing ranging between 0 and n-1, and element x that needs to be searched in the array A. This can be done by skipping some fixed number of array elements or jumping ahead by fixed number of steps in every iteration. The fundamental idea behind this searching technique is to search fewer number of elements compared to linear search algorithm (which scans every element in the array to check if it matches with the element being searched or not). Jump Search Algorithm is a relatively new algorithm for searching an element in a sorted array.
