๐Ÿ“”
Algorithms Analysis & Design
  • Algorithm Analysis & Design
  • ๐ŸšขSearching and Sorting
    • Introduction-Search
    • Linear Search
    • Binary Search
    • Interpolation Search
    • Introduction-Sorting
    • Bubble Sort
    • Selection Sort
    • Counting Sort
  • ๐Ÿ’ธGreedy Method
    • Introduction
    • Activity Selection
    • Fractional Knapsack Problem
    • Graph Colouring
  • ๐Ÿš Backtracking
    • Introduction
    • Hamiltonian Path/Cycle Problem
    • N Queen Problem
    • Rat in a Maze
    • Knight's Tour Problem
  • โš”๏ธDivide and Conquer
    • Introduction
    • Strassen's Matrix multiplication
    • Karatsuba algorithm
    • Tower of Hanoi
    • Closest Pair
  • ๐Ÿ’ฃDynamic Programming
    • Introduction
    • Longest Common Subsequence
    • Floyd-Warshall Algorithm
    • 0-1 Knapsack problem
    • Dice Throw
  • ๐Ÿ“ˆGraph
    • Introduction
    • DFS
    • Dictionary Game
    • BFS
    • Flood Fill Algorithm
    • Minesweeper Lite
  • ๐Ÿ”ขNumber Theory
    • Introduction
    • GCD
    • Factorial
    • IsPrime | School Method
    • IsPrime | Fermat's Little Theorem
    • IsPrime | Miller-Rabin Method
  • ๐ŸŒฎReferences
Powered by GitBook
On this page
  • Working of Counting Sort
  • Counting Sort Algorithm

Was this helpful?

  1. Searching and Sorting

Counting Sort

PreviousSelection SortNextIntroduction

Last updated 3 years ago

Was this helpful?

Counting sort is a sorting algorithm that sorts the elements of an array by counting the number of occurrences of each unique element in the array. The count is stored in an auxiliary array and the sorting is done by mapping the count as an index of the auxiliary array.


Working of Counting Sort

  1. Find out the maximum element (let it be max) from the given array.Given array

  2. Initialize an array of length max+1 with all elements 0. This array is used for storing the count of the elements in the array.Count array

  3. Store the count of each element at their respective index in count array For example: if the count of element 3 is 2 then, 2 is stored in the 3rd position of count array. If element "5" is not present in the array, then 0 is stored in 5th position.Count of each element stored

  4. Store cumulative sum of the elements of the count array. It helps in placing the elements into the correct index of the sorted array.Cumulative count

  5. Find the index of each element of the original array in the count array. This gives the cumulative count. Place the element at the index calculated as shown in figure below.Counting sort

  6. After placing each element at its correct position, decrease its count by one.


Counting Sort Algorithm

countingSort(array, size)
  max <- find largest element in array
  initialize count array with all zeros
  for j <- 0 to size
    find the total count of each unique element and 
    store the count at jth index in count array
  for i <- 1 to max
    find the cumulative sum and store it in count array itself
  for j <- size down to 1
    restore the elements to array
    decrease count of each element restored by 1

Given an integer array with many duplicated elements, write an algorithm to efficiently sort it in linear time, where the order of equal elements doesnโ€™t matter.

For example,

Input: { 4, 2, 40, 10, 10, 1, 4, 2, 1, 10, 40 }

A simple solution would be to use efficient sorting algorithms like Merge Sort, Quicksort, Heapsort, etc., that can solve this problem in O(n.log(n)) time, but those will not take advantage of the fact that there are many duplicated values in the array.

A better approach is to use a counting sort. This will bring down the time complexity to O(n + k), where n is the size of the input and k is the input range.

Output: { 1, 1, 2, 2, 4, 4, 10, 10, 10, 40, 40 }

๐Ÿšข
Counting Sort Steps
Counting Sort Step
Counting Sort steps
Counting Sort Step
Counting Sort Step