Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Implement Counting Sort Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Implement Counting Sort

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given an array, sort it using counting sort.

Approach

The counting sort algorithm is used to sort an array in linear time and can be used when the largest element in the array and the size of the array are of the same order of magnitude. In this algorithm, we map the frequencies of the elements of the main array into an auxiliary array and store them in sorted order. The algorithm's steps are :

  • Make an auxiliary hash array and map the frequencies of elements of the original array into it.
  • Make a new array that will store the sorted array later of the appropriate size. 
  • Iterate over all the numbers from the smallest to the largest element in the array, and add hash[A[i]] number of elements into the new array.
  • Finally, empty the new array into the old array to get our sorted array.

Make sure to handle the negative elements, if any effectively. 

Analysis

  • Time Complexity : O(n)
  • Space Complexity : O(n)

Implementation

C++
void countingSort(vector<int> &arr) {
	vector<int> output(arr.size() + 1, 0);
	int maximum = 0;
	for(int i = 0; i < arr.size(); i++) {
		maximum = max(maximum, abs(arr[i]));
	}
	vector<int> count(2 * maximum + 2, 0);
	for(auto &x: arr) {
		x += maximum;
	}
	for(int i = 0; i < arr.size(); i++) {
		count[arr[i]]++;
	}
	for(int i = 1; i <= 2 * maximum + 1; i++) {
		count[i] += count[i - 1];
	}
	for(int i = 0; i < arr.size(); i++) {
		output[count[arr[i]] - 1] = arr[i];
		count[arr[i]]--;
	}
	for(int i = 0; i < arr.size(); i++) {
		arr[i] = output[i] - maximum;
	}
}
Java
class Solution {
	void countingSort (int[] arr) {
		int[] output = new int[arr.length + 1];
		int maximum = Integer.MIN_VALUE;
		for(int i = 0; i < arr.length; i++) {
			maximum = Math.max(maximum, Math.abs(arr[i]));
		}
		int[] count = new int[2 * maximum + 2];
		for(int i = 0; i < arr.length; i++) {
			arr[i] += maximum;
		}
		for(int i = 0; i < arr.length; i++) {
			count[arr[i]]++;
		}
		for(int i = 1; i <= 2 * maximum + 1; i++) {
			count[i] += count[i - 1];
		}
		for(int i = 0; i < arr.length; i++) {
			output[count[arr[i]] - 1] = arr[i];
			count[arr[i]]--;
		}
		for(int i = 0; i < arr.length; i++) {
			arr[i] = output[i] - maximum;
		}
	}
}
Related Content
Arithmetic Sequence
Clone List with Random Pointer
Distinct Numbers in Window
Four Sum
Identical Twins
Implement Insertion Sort
Implement Merge Sort
Implement Quicksort
Inversion Count
Kth Largest Element
Longest Consecutive Sequence
Longest Subarray with Zero Sum
Longest Substring with K Unique Characters
Longest Substring Without Repeat
LRU Cache
Merge Overlapping Intervals
Merge Sorted Subarrays
Non-Repeating Element
Primes upto N
Square sorted array
Subarrays With Given XOR
Three Sum
Two Sum
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet