📜  对0、1和2的数组进行排序(简单计数)

📅  最后修改于: 2021-04-27 21:01:51             🧑  作者: Mango

给定一个包含0、1、2和2的数组A [],编写一个对A []排序的函数。函数应将全0放在最前面,然后将全1放在最后,并将全2放在最后。
例子:

Input :  {0, 1, 2, 0, 1, 2}
Output : {0, 0, 1, 1, 2, 2}

Input :  {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1}
Output : {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2}

计算0、1和2的数量。计数后,将所有0放在前面,然后是1,最后放在2放在数组中。我们遍历数组两次。时间复杂度将为O(n)。

CPP
// Simple C++ program to sort an array of 0s
// 1s and 2s.
#include 
using namespace std;
 
void sort012(int* arr, int n)
{
    // Variables to maintain the count of 0's,
    // 1's and 2's in the array
    int count0 = 0, count1 = 0, count2 = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] == 0)
            count0++;
        if (arr[i] == 1)
            count1++;
        if (arr[i] == 2)
            count2++;
    }
 
    // Putting the 0's in the array in starting.
    for (int i = 0; i < count0; i++)
        arr[i] = 0;
     
    // Putting the 1's in the array after the 0's.
    for (int i = count0; i < (count0 + count1); i++)
        arr[i] = 1;
     
    // Putting the 2's in the array after the 1's
    for (int i = (count0 + count1); i < n; i++)
        arr[i] = 2;
     
    return;
}
 
// Prints the array
void printArray(int* arr, int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << endl;
}
 
// Driver code
int main()
{
    int arr[] = { 0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    sort012(arr, n);
    printArray(arr, n);
    return 0;
}


Java
// Simple Java program
// to sort an array of 0s
// 1s and 2s.
import java.util.*;
import java.lang.*;
 
public class GfG{
 
    public static void sort012(int arr[], int n)
    {
        // Variables to maintain
        // the count of 0's,
        // 1's and 2's in the array
        int count0 = 0, count1 = 0;
        int count2 = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] == 0)
                count0++;
            if (arr[i] == 1)
                count1++;
            if (arr[i] == 2)
                count2++;
        }
     
        // Putting the 0's in the
        // array in starting.
        for (int i = 0; i < count0; i++)
            arr[i] = 0;
     
        // Putting the 1's in the
        // array after the 0's.
        for (int i = count0; i <
            (count0 + count1); i++)
            arr[i] = 1;
     
        // Putting the 2's in the
        // array after the 1's
        for (int i = (count0 + count1);
            i < n; i++)
            arr[i] = 2;
     
        printArray(arr, n);
    }
     
    // Prints the array
    public static void printArray(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
        System.out.println();
    }
     
    // Driver function
    public static void main(String argc[])
    {
     
        int arr[] = { 0, 1, 1, 0, 1, 2, 1,
                    2, 0, 0, 0, 1 };
        int n = 12;
        sort012(arr, n);
    }
}
 
// This code is contributed by Sagar Shukla


Python3
# Python C++ program to sort an array of 0s
# 1s and 2s.
import math
 
def sort012(arr, n):
 
    # Variables to maintain the count of 0's,
    # 1's and 2's in the array
    count0 = 0
    count1 = 0
    count2 = 0
    for i in range(0,n):
        if (arr[i] == 0):
            count0=count0+1
        if (arr[i] == 1):
            count1=count1+1
        if (arr[i] == 2):
            count2=count2+1
     
 
    # Putting the 0's in the array in starting.
    for i in range(0,count0):
        arr[i] = 0
     
    # Putting the 1's in the array after the 0's.
    for i in range( count0, (count0 + count1)) :
        arr[i] = 1
     
    # Putting the 2's in the array after the 1's
    for i in range((count0 + count1),n) :
        arr[i] = 2
     
    return
 
 
# Prints the array
def printArray( arr,  n):
 
    for i in range(0,n):
        print( arr[i] , end=" ")
    print()
 
 
# Driver code
arr = [ 0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1 ]
n = len(arr)
sort012(arr, n)
printArray(arr, n)
 
# This code is contributed by Gitanjali.


C#
// Simple C# program
// to sort an array of 0s
// 1s and 2s.
using System;
 
public class GfG{
 
    public static void sort012(int []arr, int n)
    {
        // Variables to maintain
        // the count of 0's,
        // 1's and 2's in the array
        int count0 = 0, count1 = 0;
        int count2 = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] == 0)
                count0++;
            if (arr[i] == 1)
                count1++;
            if (arr[i] == 2)
                count2++;
        }
     
        // Putting the 0's in the
        // array in starting.
        for (int i = 0; i < count0; i++)
            arr[i] = 0;
     
        // Putting the 1's in the
        // array after the 0's.
        for (int i = count0; i <
            (count0 + count1); i++)
            arr[i] = 1;
     
        // Putting the 2's in the
        // array after the 1's
        for (int i = (count0 + count1);
            i < n; i++)
            arr[i] = 2;
     
        printArray(arr, n);
    }
     
    // Prints the array
    public static void printArray(int []arr, int n)
    {
        for (int i = 0; i < n; i++)
            Console.Write(arr[i] + " ");
        Console.WriteLine();
    }
     
    // Driver function
    public static void Main()
    {
     
        int []arr = { 0, 1, 1, 0, 1, 2, 1,
                    2, 0, 0, 0, 1 };
        int n = 12;
        sort012(arr, n);
    }
}
 
// This code is contributed by vt_m


Javascript


Python3
# Example
#
# input = [0, 1, 2, 2, 0, 0]
# output = [0, 0, 0, 1, 2, 2]
 
inputArray = [0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1]
outputArray = []
indexOfOne = 0
for item in inputArray:
    if item == 2:
        outputArray.append(item)
    elif item == 1:
        outputArray.insert(indexOfOne, item)
        indexOfOne += 1
    elif item == 0:
        outputArray.insert(0, item)
        indexOfOne += 1
    else:
        print(" wrong value - Aborting ")
        continue
print(outputArray)


输出
0 0 0 0 0 1 1 1 1 1 2 2 

上述解决方案存在问题:

  1. 它需要两次遍历数组。
  2. 如果值是结构的一部分,则此解决方案可能不起作用。例如,考虑一种情况,其中0代表计算机科学流,1代表电子学,2代表机械学。我们有一个学生对象(或结构)的列表,我们想对它们进行排序。我们不能使用上述排序,因为我们简单地将0、1和2逐一放置。

另一种方法

Python3

# Example
#
# input = [0, 1, 2, 2, 0, 0]
# output = [0, 0, 0, 1, 2, 2]
 
inputArray = [0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1]
outputArray = []
indexOfOne = 0
for item in inputArray:
    if item == 2:
        outputArray.append(item)
    elif item == 1:
        outputArray.insert(indexOfOne, item)
        indexOfOne += 1
    elif item == 0:
        outputArray.insert(0, item)
        indexOfOne += 1
    else:
        print(" wrong value - Aborting ")
        continue
print(outputArray)
输出
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2]

解决上述问题的最佳解决方案:
对0、1和2的数组进行排序(荷兰国旗算法)