Open In App

Sorting without comparison of elements

Improve
Improve
Like Article
Like
Save
Share
Report

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 <bits/stdc++.h>
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<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++)
          cout << (i + min) << " ";
}
 
// Driver Code
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




// 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




<script>
 
    // Javascript program to sort an array
    // without comparison operator.
     
    // Represents node of a doubly linked list
    function sortArr(arr, n, min, max)
    {
        // Count of elements in given range
        let m = max - min + 1;
  
        // Count frequencies of all elements
        let c = new Array(m);
        c.fill(0);
        for (let i = 0; i < n; i++)
        {
            c[arr[i] - min]++;
        }
  
        // Traverse through range. For every
        // element, print it its count times.
        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>


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.



Last Updated : 06 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads