~/coding-interview/counting-sort-algorithm-in-java.md

Counting Sort Algorithm

2 min readJuly 16, 2019by Gorav Singal#coding-interview#sorting-algorithms#counting-sort
// TL;DR

Count occurrences of each value, compute prefix sums to determine positions, then place elements directly. O(n + k) where k is the range of input values.

Counting Sort Algorithm

Counting sort runs on relatively smaller set of input. Counting sort calculates, for every element in array - X, the number of elements which are lesser than - X. It then use this information to put X directly into its position in sorted array.

Selection sort takes two additional array.

  • One for result, the sorted array
  • other for temporary storage, where size equals to the maximum number on input array.

Its interesting to note that this algorithm is not a comparison sort. You will not see any comparison in code. This algorithm uses elements values to index into array.

Counting Sort Example

Count Sort Algorithm

  • First iterate on input array, and calculate each number occurence in the 2nd additional array (lets call it C).
  • After the iteration, The array-C’s index will denote the input number, and the value contains how many times the number comes in original array. In the image above, see part-a.
  • Run one more iteration on array-c. And, accumulate the sum. See above image, part-b
  • Now iterate equal to length of original array (from last index to 0):
    • For the number in original array, use it as index in array-C, which will give you its position in sorted array
    • Put this number in sorted array at position told by array-c
    • Decrement this value from array-c by 1
  • At the end, we will get sorted array.

See the code here:

public class CountSort {
    private int[] arr;

    public CountSort(int[] arr) {
        this.arr = arr;
    }

    /**
     * Count sort, max value-k
     * @param k, the max value in array
     */
    public int[] doSort(int k) {
        int[] c = new int[k+1];

        //count number of occurence
        for (int i=0; i<this.arr.length; i++) {
            c[this.arr[i]] ++;
        }

        //accumulate
        for (int i=1; i<c.length; i++) {
            c[i] += c[i-1];
        }
        int[] result = new int[this.arr.length];

        //put the number to its designated place
        for (int i=this.arr.length-1; i>=0; i--) {
            result[c[this.arr[i]]-1] = this.arr[i];
            c[this.arr[i]] --;
        }

        return result;
    }
}

Key Points

  • It is not a comparison sort.
  • It is a stable sort.
  • This uses 2 additional arrays.
  • It is used for relatively smaller set of numbers.
  • The maximum number in array should be order of size of array. Example: this algorithm is good for sorting age of people.
  • Performance is very good. its time complexity is O(n + k) ~= O(n)

Runtime complexity

The algorithm runs on time complexity of O(n + k) ~= O(n) in worst/average case.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Radix Sort Algorithm

Radix Sort Algorithm

A number consists of digits. Example: 843. Its a 3-digit number. Radix sort…

Heap Sort Algorithm

Heap Sort Algorithm

This is another very useful sorting algorithm based on Heap data structure. Read…

Quick Sort Algorithm

Quick Sort Algorithm

This algorithm is very useful for large input. And, is quite efficient one. It…

Merge Sort Algorithm

Merge Sort Algorithm

This algorithm is very efficient one, and is classic example of Divide and…

Bubble Sort Algorithm

Bubble Sort Algorithm

This is kind of preliminary technique of sorting. And, this is the first…

Selection Sort Algorithm

Selection Sort Algorithm

It is one of a simple algorithm to study for a beginner to understanding sorting…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…