给定一个由N个数字的递减排列组成的数组A [] ,任务是使用三重交换对数组进行排序。如果无法对数组进行排序,则打印-1。
Triple swaps refer to cyclic right shift on chosen indices. Cyclic Right Shift: x –> y –> z –> x.
例子:
Input: A[] = {4, 3, 2, 1}
Output: 1 2 3 4
Explanation:
For the given array the first step is choosing indexes: x = 0, y = 2, z = 3
Therefore, A[3] = A[2]; A[2] = A[0]; A[0] = A[3].
Before Swapping: 4 3 2 1 and After Swapping: 1 3 4 2.
For the given array the second step is choosing indexes: x = 1, y = 2, z = 3 Therefore, A[3] = A[2]; A[2] = A[1]; A[1] = A[3].
Before Swapping: 1 3 4 2 and After Swapping: 1 2 3 4.
Input: A[] = {5, 4, 3, 2, 1}
Output: 1 2 3 4 5
Explanation:
For the given array the first step is choosing indexes: x = 0, y = 3, z = 4 therefore,
A[4] = A[3]; A[3] = A[0]; A[0] = A[4], Before Swapping: 5 4 3 2 1 and After Swapping: 1 4 3 5 2
For the given array the second step is choosing indexes: x = 1, y = 3, z = 4 therefore,
A[4] = A[3]; A[3] = A[1]; A[1] = A[4], Before Swapping: 1 4 3 5 2 and After Swapping: 1 2 3 4 5
方法:
为了解决上述问题,我们必须以这种方式选择三个索引,以便我们可以将至少一个元素放在正确的位置。这样,我们的意思是必须在索引0处带1,在索引1处带2,依此类推。
- 选择x作为当前索引编号i,
- 选择z作为x + 1的索引,该索引始终为N – i – 1,并且
- 相应地选择y。
然后,我们必须使用这些索引通过元素的循环右移来执行元素交换。
下面是上述方法的实现:
C++
// C++ implementation to sort
// decreasing permutation of N
// using triple swaps
#include
using namespace std;
// Function to sort Array
void sortArray(int A[], int N)
{
// The three indices that
// has to be chosen
int x, y, z;
// Check if possible to sort array
if (N % 4 == 0 || N % 4 == 1) {
// Swapping to bring element
// at required position
// Bringing at least one
// element at correct position
for (int i = 0; i < N / 2; i++) {
x = i;
if (i % 2 == 0) {
y = N - i - 2;
z = N - i - 1;
}
// Tracing changes in Array
A[z] = A[y];
A[y] = A[x];
A[x] = x + 1;
}
// Print the sorted array
cout << "Sorted Array: ";
for (int i = 0; i < N; i++)
cout << A[i] << " ";
}
// If not possible to sort
else
cout << "-1";
}
// Driver code
int main()
{
int A[] = { 5, 4, 3, 2, 1 };
int N = sizeof(A) / sizeof(A[0]);
sortArray(A, N);
return 0;
}
Java
// Java implementation to sort
// decreasing permutation of N
// using triple swaps
class GFG{
// Function to sort array
static void sortArray(int A[], int N)
{
// The three indices that
// has to be chosen
int x = 0, y = 0, z = 0;
// Check if possible to sort array
if (N % 4 == 0 || N % 4 == 1)
{
// Swapping to bring element
// at required position
// Bringing at least one
// element at correct position
for(int i = 0; i < N / 2; i++)
{
x = i;
if (i % 2 == 0)
{
y = N - i - 2;
z = N - i - 1;
}
// Tracing changes in array
A[z] = A[y];
A[y] = A[x];
A[x] = x + 1;
}
// Print the sorted array
System.out.print("Sorted Array: ");
for(int i = 0; i < N; i++)
System.out.print(A[i] + " ");
}
// If not possible to sort
else
{
System.out.print("-1");
}
}
// Driver code
public static void main(String[] args)
{
int A[] = { 5, 4, 3, 2, 1 };
int N = A.length;
sortArray(A, N);
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 implementation to sort
# decreasing permutation of N
# using triple swaps
# Function to sort array
def sortArray(A, N):
# Check if possible to sort array
if (N % 4 == 0 or N % 4 == 1):
# Swapping to bring element
# at required position
# Bringing at least one
# element at correct position
for i in range(N // 2):
x = i
if (i % 2 == 0):
y = N - i - 2
z = N - i - 1
# Tracing changes in Array
A[z] = A[y]
A[y] = A[x]
A[x] = x + 1
# Print the sorted array
print("Sorted Array: ", end = "")
for i in range(N):
print(A[i], end = " ")
# If not possible to sort
else:
print("-1")
# Driver code
A = [ 5, 4, 3, 2, 1 ]
N = len(A)
sortArray(A, N)
# This code is contributed by yatinagg
C#
// C# implementation to sort
// decreasing permutation of N
// using triple swaps
using System;
class GFG{
// Function to sort array
static void sortArray(int []A, int N)
{
// The three indices that
// has to be chosen
int x = 0, y = 0, z = 0;
// Check if possible to sort array
if (N % 4 == 0 || N % 4 == 1)
{
// Swapping to bring element
// at required position
// Bringing at least one
// element at correct position
for(int i = 0; i < N / 2; i++)
{
x = i;
if (i % 2 == 0)
{
y = N - i - 2;
z = N - i - 1;
}
// Tracing changes in array
A[z] = A[y];
A[y] = A[x];
A[x] = x + 1;
}
// Print the sorted array
Console.Write("Sorted Array: ");
for(int i = 0; i < N; i++)
Console.Write(A[i] + " ");
}
// If not possible to sort
else
{
Console.Write("-1");
}
}
// Driver code
public static void Main(String[] args)
{
int []A = { 5, 4, 3, 2, 1 };
int N = A.Length;
sortArray(A, N);
}
}
// This code is contributed by sapnasingh4991
Javascript
Sorted Array: 1 2 3 4 5