Skip to content
Related Articles
Sorting without comparison of elements
• Difficulty Level : Easy
• Last Updated : 06 May, 2021

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.

1. All the entries in the array are integers.
2. 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++

 `// C++ program to sort an array without comparison``// operator.``#include ``using` `namespace` `std;` `int` `sortArr(``int` `arr[], ``int` `n, ``int` `min, ``int` `max)``{``    ``// Count of elements in given range``    ``int` `m = max - min + 1;``    ` `    ``// Count frequencies of all elements``    ``vector<``int``> c(m, 0);``    ``for` `(``int` `i=0; i

## Java

 `// Java program to sort an array without comparison``// operator.``import` `java.util.*;` `// Represents node of a doubly linked list``class` `Node``{` `    ``static` `void` `sortArr(``int` `arr[], ``int` `n, ``int` `min, ``int` `max)``    ``{``        ``// Count of elements in given range``        ``int` `m = max - min + ``1``;` `        ``// Count frequencies of all elements``        ``int``[] c = ``new` `int``[m];``        ``for` `(``int` `i = ``0``; i < n; i++)``        ``{``            ``c[arr[i] - min]++;``        ``}` `        ``// Traverse through range. For every``        ``// element, print it its count times.``        ``for` `(``int` `i = ``0``; i < m; i++)``        ``{``            ``for` `(``int` `j = ``0``; j < c[i]; j++)``            ``{``                ``System.out.print((i + min) + ``" "``);``            ``}``        ``}``    ``}``  ` `    ``// Driver Code``    ``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);``    ``}``}` `// This code is contributed by Princi Singh`

## Python3

 `# Python3 program to sort an array without comparison``# operator.` `def` `sortArr(arr, n, min_no, max_no):``    ``# Count of elements in given range``    ``m ``=` `max_no ``-` `min_no ``+` `1``    ` `    ``# Count frequencies of all elements``    ``c ``=` `[``0``] ``*` `m``    ``for` `i ``in` `range``(n):``        ``c[arr[i] ``-` `min_no] ``+``=` `1` `    ``# Traverse through range. For every``    ``# element, print it its count times.``    ``for` `i ``in` `range``(m):``        ``for` `j ``in` `range``((c[i])):``            ``print``((i ``+` `min_no), end``=``" "``)` `# Driver Code``arr ``=` `[``10``, ``10``, ``1``, ``4``, ``4``, ``100``, ``0``]``min_no,max_no ``=` `0``,``100``n ``=` `len``(arr)``sortArr(arr, n, min_no, max_no)` `# This code is contributed by Rajput-Ji``# Improved by Rutvik J`

## C#

 `// C# program to sort an array``// without comparison operator.``using` `System;` `class` `GFG``{``    ` `    ``// Represents node of a doubly linked list``    ``static` `void` `sortArr(``int` `[]arr, ``int` `n,``                        ``int` `min, ``int` `max)``    ``{``        ``// Count of elements in given range``        ``int` `m = max - min + 1;` `        ``// Count frequencies of all elements``        ``int``[] c = ``new` `int``[m];``        ``for` `(``int` `i = 0; i < n; i++)``        ``{``            ``c[arr[i] - min]++;``        ``}` `        ``// Traverse through range. For every``        ``// element, print it its count times.``        ``for` `(``int` `i = 0; i < m; i++)``        ``{``            ``for` `(``int` `j = 0; j < c[i]; j++)``            ``{``                ``Console.Write((i + min) + ``" "``);``            ``}``        ``}``    ``}` `    ``// Driver Code``    ``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);``    ``}``}` `// This code is contributed by ajit.`

## Javascript

 ``
Output
`0 1 4 4 10 10 100`

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up