📌  相关文章
📜  对 0、1、2 和 3 的数组进行排序

📅  最后修改于: 2021-09-04 07:54:23             🧑  作者: Mango

给定一个大小为N的数组arr[] 仅0123 组成,任务是按升序对给定数组进行排序。

例子:

方法:可以根据本文中讨论的方法解决给定的问题。这个想法是首先将所有的03放在数组的开头和结尾,然后对出现的整数12进行排序。

请按照以下步骤解决问题:

  • 初始化三个变量,比如imidj 。将imid的值设置为0 ,将j 设置(N – 1)
  • 迭代一个循环直到mid ≤ j并执行以下步骤:
    • 如果arr[mid] 的值为0 ,则交换arr[i]arr[mid]并将imid的值增加1
    • 否则,如果arr[mid] 的值为3 ,则交换arr[mid]arr[j]并将j1
    • 否则,如果arr[i] 的值是12 ,则将mid的值增加1
  • 现在通过迭代直到i ≤ j对范围[i, j] 上的子数组进行排序并执行以下操作:
    • 如果arr[i] 的值为2 ,则交换arr[i]arr[j]并递减 j的值乘以1
    • 否则,将i的值增加1
  • 完成上述步骤后,打印数组arr[]作为结果排序数组。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to sort the array having
// array element only 0, 1, 2, and 3
void sortArray(int arr[], int N)
{
    int i = 0, j = N - 1, mid = 0;
 
    // Iterate until mid <= j
    while (mid <= j) {
 
        // If arr[mid] is 0
        if (arr[mid] == 0) {
 
            // Swap integers at
            // indices i and mid
            swap(arr[i], arr[mid]);
 
            // Increment i
            i++;
 
            // Increment mid
            mid++;
        }
 
        // Otherwise if the value of
        // arr[mid] is 3
        else if (arr[mid] == 3) {
 
            // Swap arr[mid] and arr[j]
            swap(arr[mid], arr[j]);
 
            // Decrement j
            j--;
        }
 
        // Otherwise if the value of
        // arr[mid] is either 1 or 2
        else if (arr[mid] == 1
                 || arr[mid] == 2) {
 
            // Increment the value of mid
            mid++;
        }
    }
 
    // Iterate until i <= j
    while (i <= j) {
 
        // If arr[i] the value of is 2
        if (arr[i] == 2) {
 
            // Swap arr[i] and arr[j]
            swap(arr[i], arr[j]);
 
            // Decrement j
            j--;
        }
 
        // Otherwise, increment i
        else {
            i++;
        }
    }
 
    // Print the sorted array arr[]
    for (int i = 0; i < N; i++) {
        cout << arr[i] << " ";
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 2, 1, 0, 2, 3, 1, 0 };
    int N = sizeof(arr) / sizeof(arr[0]);
    sortArray(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to sort the array having
// array element only 0, 1, 2, and 3
static void sortArray(int[] arr, int N)
{
    int i = 0, j = N - 1, mid = 0;
 
    // Iterate until mid <= j
    while (mid <= j)
    {
         
        // If arr[mid] is 0
        if (arr[mid] == 0)
        {
             
            // Swap integers at
            // indices i and mid
            int temp = arr[i];
            arr[i] = arr[mid];
            arr[mid] = temp;
 
            // Increment i
            i++;
 
            // Increment mid
            mid++;
        }
 
        // Otherwise if the value of
        // arr[mid] is 3
        else if (arr[mid] == 3)
        {
             
            // Swap arr[mid] and arr[j]
            int temp = arr[mid];
            arr[mid] = arr[j];
            arr[j] = temp;
 
            // Decrement j
            j--;
        }
 
        // Otherwise if the value of
        // arr[mid] is either 1 or 2
        else if (arr[mid] == 1 || arr[mid] == 2)
        {
             
            // Increment the value of mid
            mid++;
        }
    }
 
    // Iterate until i <= j
    while (i <= j)
    {
         
        // If arr[i] the value of is 2
        if (arr[i] == 2)
        {
             
            // Swap arr[i] and arr[j]
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
 
            // Decrement j
            j--;
        }
 
        // Otherwise, increment i
        else
        {
            i++;
        }
    }
 
    // Print the sorted array arr[]
    for(int k = 0; k < N; k++)
    {
        System.out.print(arr[k] + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int[] arr = { 3, 2, 1, 0, 2, 3, 1, 0 };
    int N = arr.length;
     
    sortArray(arr, N);
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program for the above approach
 
# Function to sort the array having
# array element only 0, 1, 2, and 3
def sortArray(arr, N):
    i = 0
    j = N - 1
    mid = 0
 
    # Iterate until mid <= j
    while (mid <= j):
 
        # If arr[mid] is 0
        if (arr[mid] == 0):
 
            # Swap integers at
            # indices i and mid
            arr[i], arr[mid] = arr[mid], arr[i]
 
            # Increment i
            i += 1
             
            # Increment mid
            mid += 1
 
        # Otherwise if the value of
        # arr[mid] is 3
        elif (arr[mid] == 3):
 
            # Swap arr[mid] and arr[j]
            arr[mid], arr[j] = arr[j], arr[mid]
             
            # Decrement j
            j -= 1
 
        # Otherwise if the value of
        # arr[mid] is either 1 or 2
        elif (arr[mid] == 1 or arr[mid] == 2):
            # Increment the value of mid
            mid += 1
 
    # Iterate until i <= j
    while (i <= j):
       
        # If arr[i] the value of is 2
        if (arr[i] == 2):
           
            # Swap arr[i] and arr[j]
            arr[i], arr[j] = arr[j], arr[i]
             
            # Decrement j
            j -= 1
 
        # Otherwise, increment i
        else:
            i += 1
 
    # Print the sorted array arr[]
    for i in range(N):
        print(arr[i], end = " ")
 
# Driver Code
if __name__ == '__main__':
    arr = [3, 2, 1, 0, 2, 3, 1, 0]
    N = len(arr)
    sortArray(arr, N)
 
#  This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to sort the array having
// array element only 0, 1, 2, and 3
static void sortArray(int[] arr, int N)
{
    int i = 0, j = N - 1, mid = 0;
 
    // Iterate until mid <= j
    while (mid <= j)
    {
         
        // If arr[mid] is 0
        if (arr[mid] == 0)
        {
             
            // Swap integers at
            // indices i and mid
            int temp = arr[i];
            arr[i] = arr[mid];
            arr[mid] = temp;
 
            // Increment i
            i++;
 
            // Increment mid
            mid++;
        }
 
        // Otherwise if the value of
        // arr[mid] is 3
        else if (arr[mid] == 3)
        {
             
            // Swap arr[mid] and arr[j]
            int temp = arr[mid];
            arr[mid] = arr[j];
            arr[j] = temp;
 
            // Decrement j
            j--;
        }
 
        // Otherwise if the value of
        // arr[mid] is either 1 or 2
        else if (arr[mid] == 1 || arr[mid] == 2)
        {
             
            // Increment the value of mid
            mid++;
        }
    }
 
    // Iterate until i <= j
    while (i <= j)
    {
         
        // If arr[i] the value of is 2
        if (arr[i] == 2)
        {
             
            // Swap arr[i] and arr[j]
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
 
            // Decrement j
            j--;
        }
 
        // Otherwise, increment i
        else
        {
            i++;
        }
    }
 
    // Print the sorted array arr[]
    for(int k = 0; k < N; k++)
    {
        Console.Write(arr[k] + " ");
    }
}
 
// Driver Code
public static void Main()
{
    int[] arr = { 3, 2, 1, 0, 2, 3, 1, 0 };
    int N = arr.Length;
     
    sortArray(arr, N);
}
}
 
// This code is contributed by ukasp


Javascript


输出:
0 0 1 1 2 2 3 3

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live