
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Implement Counting Sort
Counting sort is a stable sorting technique, which is used to sort objects according the keys that are small numbers. It counts the number of keys whose key values are same. This sorting technique is efficient when difference between different keys are not so big, otherwise it can increase the space complexity.
The complexity of counting Sort Technique
Time Complexity: O(n+r)
Space Complexity: O(n+r)
Input − A list of unsorted data: 2 5 6 2 3 10 3 6 7 8
Output − Array after Sorting: 2 2 3 3 5 6 6 7 8 10
Algorithm
countingSort(array, size)
Input: An array of data, and the total number in the array
Output: The sorted Array
Begin max = get maximum element from array. define count array of size [max+1] for i := 0 to max do count[i] = 0 //set all elements in the count array to 0 done for i := 1 to size do increase count of each number which have found in the array done for i := 1 to max do count[i] = count[i] + count[i+1] //find cumulative frequency done 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 Code
#include<iostream> #include<algorithm> using namespace std; void display(int *array, int size) { for(int i = 1; i<=size; i++) cout << array[i] << " "; cout << endl; } int getMax(int array[], int size) { int max = array[1]; for(int i = 2; i<=size; i++) { if(array[i] > max) max = array[i]; } return max; //the max element from the array } void countSort(int *array, int size) { int output[size+1]; int max = getMax(array, size); int count[max+1]; //create count array (max+1 number of elements) for(int i = 0; i<=max; i++) count[i] = 0; //initialize count array to all zero for(int i = 1; i <=size; i++) count[array[i]]++; //increase number count in count array. for(int i = 1; i<=max; i++) count[i] += count[i-1]; //find cumulative frequency for(int i = size; i>=1; i--) { output[count[array[i]]] = array[i]; count[array[i]] -= 1; //decrease count for same numbers } for(int i = 1; i<=size; i++) { array[i] = output[i]; //store output array to main array } } int main() { int n; cout << "Enter the number of elements: "; cin >> n; int arr[n+1]; //create an array with given number of elements cout << "Enter elements:" << endl; for(int i = 1; i<=n; i++) { cin >> arr[i]; } cout << "Array before Sorting: "; display(arr, n); countSort(arr, n); cout << "Array after Sorting: "; display(arr, n); }
Output
Enter the number of elements: 10 Enter elements: 2 5 6 2 3 10 3 6 7 8 Array before Sorting: 2 5 6 2 3 10 3 6 7 8 Array after Sorting: 2 2 3 3 5 6 6 7 8 10
- Related Articles
- Python Program for Counting Sort
- Java Program for Counting Sort
- Counting Sort
- Java program to implement bubble sort
- Java program to implement selection sort
- Java program to implement insertion sort
- C++ Program to Implement Radix Sort
- C++ Program to Implement Bucket Sort
- C++ Program to Implement Bubble Sort
- C++ Program to Implement Heap Sort
- C++ Program to Implement Merge Sort
- C++ Program to Implement Selection Sort
- C++ Program to Implement Insertion Sort
- C++ Program to Implement Shell Sort
- Python Program to Implement Shell Sort

Advertisements