Given an array with integer elements in small range, sort the array. We need to write a non-comparison based sorting algorithm with following assumptions about input.
- All the entries in the array are integers.
- The difference between the maximum value and the minimum value in the array is less than or equal to 10^6.
Input : arr[] = {10, 30, 20, 4}
Output : 4 10 20 30
Input : arr[] = {10, 30, 1, 20, 4}
Output : 1 4 10 20 30
We are not allowed to use comparison based sorting algorithms like QuickSort, MergeSort, etc.
Since elements are small, we use array elements as index. We store element counts in a count array. Once we have count array, we traverse the count array and print every present element its count times.
C++
#include <bits/stdc++.h>
using namespace std;
int sortArr( int arr[], int n, int min, int max)
{
int m = max - min + 1;
vector< int > c(m, 0);
for ( int i=0; i<n; i++)
c[arr[i] - min]++;
for ( int i=0; i<m; i++)
for ( int j=0; j < c[i]; j++)
cout << (i + min) << " " ;
}
int main()
{
int arr[] = {10, 10, 1, 4, 4, 100, 0};
int min = 0, max = 100;
int n = sizeof (arr)/ sizeof (arr[0]);
sortArr(arr, n, min, max);
return 0;
}
|
Java
import java.util.*;
class Node
{
static void sortArr( int arr[], int n, int min, int max)
{
int m = max - min + 1 ;
int [] c = new int [m];
for ( int i = 0 ; i < n; i++)
{
c[arr[i] - min]++;
}
for ( int i = 0 ; i < m; i++)
{
for ( int j = 0 ; j < c[i]; j++)
{
System.out.print((i + min) + " " );
}
}
}
public static void main(String[] args)
{
int arr[] = { 10 , 10 , 1 , 4 , 4 , 100 , 0 };
int min = 0 , max = 100 ;
int n = arr.length;
sortArr(arr, n, min, max);
}
}
|
Python3
def sortArr(arr, n, min_no, max_no):
m = max_no - min_no + 1
c = [ 0 ] * m
for i in range (n):
c[arr[i] - min_no] + = 1
for i in range (m):
for j in range ((c[i])):
print ((i + min_no), end = " " )
arr = [ 10 , 10 , 1 , 4 , 4 , 100 , 0 ]
min_no,max_no = 0 , 100
n = len (arr)
sortArr(arr, n, min_no, max_no)
|
C#
using System;
class GFG
{
static void sortArr( int []arr, int n,
int min, int max)
{
int m = max - min + 1;
int [] c = new int [m];
for ( int i = 0; i < n; i++)
{
c[arr[i] - min]++;
}
for ( int i = 0; i < m; i++)
{
for ( int j = 0; j < c[i]; j++)
{
Console.Write((i + min) + " " );
}
}
}
static public void Main ()
{
int []arr = {10, 10, 1, 4, 4, 100, 0};
int min = 0, max = 100;
int n = arr.Length;
sortArr(arr, n, min, max);
}
}
|
Javascript
<script>
function sortArr(arr, n, min, max)
{
let m = max - min + 1;
let c = new Array(m);
c.fill(0);
for (let i = 0; i < n; i++)
{
c[arr[i] - min]++;
}
for (let i = 0; i < m; i++)
{
for (let j = 0; j < c[i]; j++)
{
document.write((i + min) + " " );
}
}
}
let arr = [10, 10, 1, 4, 4, 100, 0];
let min = 0, max = 100;
let n = arr.length;
sortArr(arr, n, min, max);
</script>
|
What is time complexity?
Time complexity of above algorithm is O(n + (max-min))
Is above algorithm stable?
The above implementation is not stable as we do not care about order of same elements while sorting.
How to make above algorithm stable?
The stable version of above algorithm is called Counting Sort. In counting sort, we store sums of all smaller or equal values in c[i] so that c[i] store actual position of i in sorted array. After filling c[], we traverse input array again, place every element at its position and decrement count.
What are non-comparison based standard algorithms?
Counting Sort, Radix Sort and Bucket Sort.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!