给定一个大小为N的数组arr[] 仅由0 、 1 、 2和3 组成,任务是按升序对给定数组进行排序。
例子:
Input: arr[] = {0, 3, 1, 2, 0, 3, 1, 2}
Output: 0 0 1 1 2 2 3 3
Input: arr[] = {0, 1, 3, 1, 0, 1, 3, 2, 1, 2, 0, 3, 0, 0, 1}
Output: 0 0 0 0 0 1 1 1 1 1 2 2 3 3 3
方法:可以根据本文中讨论的方法解决给定的问题。这个想法是首先将所有的0和3放在数组的开头和结尾,然后对出现的整数1和2进行排序。
请按照以下步骤解决问题:
- 初始化三个变量,比如i 、 mid和j 。将i和mid的值设置为0 ,将j 设置为(N – 1) 。
- 迭代一个循环直到mid ≤ j并执行以下步骤:
- 如果arr[mid] 的值为0 ,则交换arr[i]和arr[mid]并将i和mid的值增加1 。
- 否则,如果arr[mid] 的值为3 ,则交换arr[mid]和arr[j]并将j减1 。
- 否则,如果arr[i] 的值是1或2 ,则将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