×

Counting Sort

Counting Sort: Counting sort is a sorting algorithm that is used to sort the elements of the array within a specific range. It counts the same element number of the array, and stores these same elements in the auxiliary array. It was developed by Harold H. Seward in 1954.

Complexity table of counting sort

ComplexityBest caseAverage caseWorst case
Time? (n + k)? (n + k)O (n + k)
Space  O S(nk)

Where n is the number of the element, and k is the range of the input.

Algorithm of counting sort

Counting_Sort (array, size)  max ? find the largest element from the array.    define count array length [largest + 1].      for i ? 0 to max do       count[i] = 0 // initialize count array with all zeros.      for i ? 1 to size do       increase count of each number which has found in the array.      for i ? 1 to max do       count[i] = count[i] + count[i+1] // cumulative sum.      for i ? size to 1 decrease by 1 do       store the number in the output array.       decrease count[i]    done      return the output array End  

Example 1: Suppose we have the following array, which we have to sort.

55418223

Step 1: Find the largest element in the given array.             

Largest element is = 8   5 5 4 1 8 2 2 3  

Step 2: Initialize a new array, and the size of the new array is (Largest + 1).  

Counting Sort in DS

Step 3: Count each element in the given array, and stores the element at their respective index in the count array. For example: If 5 is assigned 2 times to the original array, then 2 will be stored at the 5th position in the count array. If 6 is assigned 0 times, then 0 will be stored at the 6th position in the count array. 

Counting Sort in DS

Step 4: Make the cumulative sum of the elements of the count array. This helps to place the elements in the correct index of the sorted array.  

Counting Sort in DS

Step 5:  This process will be implemented throughout the array.  

Counting Sort in DS
Counting Sort in DS
Counting Sort in DS
Counting Sort in DS
Counting Sort in DS

Counting sort program in C language:

  #include <stdio.h>   
void countingSort(int array[], int size)
 {   
int output[10];   
int max = array[0]; 
  for (int i = 1; i < size; i++) 
{    
 if (array[i] > max)      max = array[i];  
}     
int count[10];  
 for (int i = 0; i <= max; ++i)
{   
  count[i] = 0;  
 }    
 for (int i = 0; i < size; i++) 
{  
   count[array[i]]++;  
 }   
  for (int i = 1; i <= max; i++)
 {   
  count[i] += count[i - 1]; 
  }  
   for (int i = size - 1; i >= 0; i--) 
{    
 output[count[array[i]] - 1] = array[i]; 
    count[array[i]]--; 
  } 
   for (int i = 0; i < size; i++) 
{   
  array[i] = output[i]; 
  } 
}  
 // Function to print an array void printArray(int array[], int size)
 {  
 for (int i = 0; i < size; ++i)
 {  
   printf("%d  ", array[i]); 
  } 
  printf("\n"); 
}   // Driver code int main() 
{ 
  int array[] = {5, 5, 4, 1, 3, 8};
  int n = sizeof(array) / sizeof(array[0]);
   countingSort(array, n); 
  printArray(array, n); 
}

Related Topics

Hashing and its Applications

Hashing Hashing refers to transforming plain text data in such a way that even if it is leaked for some reason, no one would be able to make sense of it....

6 minutes read.

Left View of Binary Tree

Implementation // creating a C++ program to print the Left view of the binary tree. #include <bits/stdc++.h> using namespace std; struct Nod { int record; struct Nod *Lft, *Rt; }; // creating a utility function that will eventually help...

4 minutes read.

Function to Insert a Node in a Binary Search Tree

Implementation // writing C++ code that will help us in implementing the insertion operation in a binary search tree. #include <bits/stdc++.h> using namespace std; // creating a new binary search tree node struct __nod { int...

8 minutes read.

Tree vs Graph: Data Structure

Difference Between Tree and Graph What is Tree? A tree is a non-linear data structure and finite collection of elements called node. A tree, in which the data items are arranged in...

3 minutes read.

Optimal binary search tree in DSA

Implementation // A simple way of the recursive implementation of the optimal search that we will perform on the binary tree.   #include <bits/stdc++.h> using namespace std; // we have to create a basic utility...

8 minutes read.

Heap Sort in Data Structure

Heap Sort: Heap Sort is very useful and efficient sorting algorithm in data structure. We can say it is a comparison base sorting algorithm, similar sort where we will find...

2 minutes read.

Understanding Data Processing

Introduction Data In our everyday lives, any task that we perform online is related to data. Millions of pieces of data are produced every second across the globe. Data production is largely...

4 minutes read.

Given a Binary Tree Check the Zig-Zag Traversal

Implementation // The C++ implementation of the zig-zag traversal method in the O(n) time.  #include <iostream> #include <stack> using namespace std; // creating a binary tree node. struct __nod { int record; struct __nod *Lft, *Rt; }; // creating a...

4 minutes read.

Insertion sort

Insertion sort is a simple sorting technique. It is best suited for small data sets, but it does not suitable for large data sets. In this technique, we pick an...

4 minutes read.

Strictly binary tree in Data Structures?

What is a strictly Binary Tree in Data Structures? There are various kinds of binary trees that we know exist in data structures, and they all have their purposes. In this...

4 minutes read.

Advantages and Disadvantages of Linked List

Advantages of Linked List The linked list is a dynamic data structure.You can also decrease and increase the linked list at run-time. That is, you can allocate and deallocate memory at...

3 minutes read.

Equal Sum

Find an element in array such that the sum of left array is equal to the sum of right array You have been given an array of numbers. You have to...

4 minutes read.

Binary search tree traversal in-order pre-order post-order examples

A binary search tree is a type of non-linear tree in which the tree contains at least two nods. It is called binary because of its nature that states bi...

8 minutes read.

Rearrange a linked list into alternate fashion first and the last element

Rearrange a linked list into alternate fashion first and the last element This article will explain how to rearrange the linked list into alternate fashion first and the last element. Here,...

3 minutes read.

Given a Perfect Binary Tree, Reverse Alternate Levels

Implementation //writing a program in C++ language to see how to approach it. #include <bits/stdc++.h> using namespace std; // creating a tree node. struct Nod { char ky; struct Nod *Lft, *Rt; }; // creating a new utility function...

9 minutes read.

A Full Binary Tree with n Nodes

Implementation // Writing the implementation of the above approach in C++ #include <bits/stdc++.h> using namespace std; // We are creating a class that will create a node and its left and right children.  struct __nod...

12 minutes read.

Binary Tree vs Binary Search Tree: Data Structure

Difference Between Binary Tree and Binary Search Tree What is Binary Tree? A tree which each node can have utmost two children called binary tree. These children are referred as the ‘left...

3 minutes read.

What is the B+ Tree in Data Structures?

We all know that the B+ tree in data structures is nothing but just an extended version of the B tree. It allows the smooth working of all the operations...

7 minutes read.

Properties of Binary Tree

Trees are maybe of the most significant datum structures. They are used to store and figure out data. A binarytree is a tree data structure made from nodes, all of which has...

3 minutes read.

Hashing

Hashing: Hashing is a process in which a large amount of data is mapped to a small table with the help of hashing function. It is a searching technique. Hash table We...

4 minutes read.