📜  排序时不比较元素

📅  最后修改于: 2021-04-24 20:06:27             🧑  作者: Mango

给定一个数组,该数组的整数元素在较小范围内,请对该数组进行排序。我们需要编写一个基于非比较的排序算法,并具有以下关于输入的假设。

  1. 数组中的所有条目都是整数。
  2. 数组中的最大值和最小值之差小于或等于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

我们不允许使用基于比较的排序算法,例如QuickSort,MergeSort等。
由于元素很小,因此我们将数组元素用作索引。我们将元素计数存储在count数组中。一旦有了count数组,我们将遍历count数组并打印每个当前元素的计数时间。

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


输出
0 1 4 4 10 10 100

什么是时间复杂度?
上述算法的时间复杂度为O(n +(max-min))

以上算法稳定吗?
上面的实现方式不稳定,因为我们不在乎排序时相同元素的顺序。

如何使上述算法稳定?
上述算法的稳定版本称为计数排序。在计数排序时,我们将所有较小或相等值的和存储在c [i]中,以便c [i]将i的实际位置存储在排序数组中。填充c []之后,我们再次遍历输入数组,将每个元素放置在其位置并减少计数。

什么是非比较标准算法?
计数排序,基数排序和存储桶排序。