📌  相关文章
📜  通过交换相邻元素来打乱每个 Array 元素的位置

📅  最后修改于: 2021-09-02 05:57:08             🧑  作者: Mango

给定一个数组arr[] ,任务是通过交换相邻元素来重新排列数组元素,以便交换后没有元素保留在同一位置。

例子:

方法:问题中的关键观察是数组交换数组元素可能有两种情况:

  • 如果数组的长度是偶数,那么我们可以轻松地交换 2 个变量,而无需为每对连续元素使用第三个变量。
  • 如果数组的长度是奇数,那么我们可以像上面一样做,但是最后 3 个元素不会形成一对,所以我们可以轻松地交换这 3 个变量而不使用第 4 个变量。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to print the array
void print(int a[], int n)
{
    // Loop to iterate over the
    // elements of array
    for (int i = 0; i < n; i++)
        cout << a[i] << " ";
}
 
// Function to swap two variables without
// using the third variable
void swapTwo(int& x, int& y)
{
    // Store XOR of all in x
    x = x ^ y;
 
    // After this, y has value of x
    y = x ^ y;
 
    // After this, x has value of y
    x = x ^ y;
}
 
// Function to swap three variables
// without using fourth variable
void swapThree(int& a, int& b, int& c)
{
    // Store XOR of all in a
    a = a ^ b ^ c;
 
    // After this, b has value of a
    b = a ^ b ^ c;
 
    // After this, c has value of b
    c = a ^ b ^ c;
 
    // After this, a has value of c
    a = a ^ b ^ c;
}
 
// Function that swaps n integers
void rearrangeArray(int a[], int n)
{
    if (n % 2 == 0) {
 
        for (int i = 0; i < n - 1; i += 2) {
            // Swap 2 variables without
            // using 3rd variable
            swapTwo(a[i], a[i + 1]);
        }
    }
    else {
        for (int i = 0; i < n - 3; i += 2) {
            // Swap 2 variables without
            // using 3rd variable
            swapTwo(a[i], a[i + 1]);
        }
 
        // The last 3 elements will not form
        // pair if n is odd
        // Hence, swapp 3 variables without
        // using 4th variable
        swapThree(a[n - 1], a[n - 2], a[n - 3]);
    }
 
    // Print the array elements
    print(a, n);
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    rearrangeArray(arr, n);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
 
class GFG{
     
// Function to print the array
static void print(int a[], int n)
{
     
    // Loop to iterate over the
    // elements of array
    for(int i = 0; i < n; i++)
        System.out.print(a[i] + " ");
}
 
// Function to swap two variables without
// using the third variable
static void swapTwo(int x, int y, int[] a)
{
     
    // Store XOR of all in x
    a[x] = a[x] ^ a[y];
 
    // After this, y has value of x
    a[y] = a[x] ^ a[y];
 
    // After this, x has value of y
    a[x] = a[x] ^ a[y];
}
 
// Function to swap three variables
// without using fourth variable
static void swapThree(int x, int y,
                      int z, int[] a)
{
     
    // Store XOR of all in a
    a[x] = a[x] ^ a[y] ^ a[z];
 
    // After this, b has value of a
    a[y] = a[x] ^ a[y] ^ a[z];
 
    // After this, c has value of b
    a[z] = a[x] ^ a[y] ^ a[z];
 
    // After this, a has value of c
    a[x] = a[x] ^ a[y] ^ a[z];
}
 
// Function that swaps n integers
static void rearrangeArray(int a[], int n)
{
    if (n % 2 == 0)
    {
        for(int i = 0; i < n - 1; i += 2)
        {
             
            // Swap 2 variables without
            // using 3rd variable
            swapTwo(i, i + 1, a);
        }
    }
    else
    {
        for(int i = 0; i < n - 3; i += 2)
        {
             
            // Swap 2 variables without
            // using 3rd variable
            swapTwo(i, i + 1, a);
        }
 
        // The last 3 elements will not form
        // pair if n is odd
        // Hence, swapp 3 variables without
        // using 4th variable
        swapThree(n - 1, n - 2, n - 3, a);
    }
 
    // Print the array elements
    print(a, n);
}
     
// Driver code
public static void main (String[] args)
{
     
    // Given array arr[]
    int arr[] = { 1, 2, 3, 4, 5 };
    int n = arr.length;
     
    // Function call
    rearrangeArray(arr, n);    
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
 
# Function to print the array
def print1(a, n):
 
    # Loop to iterate over the
    # elements of array
    for i in range(n):
        print(a[i], end = " ")
 
# Function to swap two variables without
# using the third variable
def swapTwo(x, y):
 
    # Store XOR of all in x
    x = x ^ y
 
    # After this, y has value of x
    y = x ^ y
 
    # After this, x has value of y
    x = x ^ y
     
    return x, y
 
# Function to swap three variables
# without using fourth variable
def swapThree(a, b, c):
 
    # Store XOR of all in a
    a = a ^ b ^ c
 
    # After this, b has value of a
    b = a ^ b ^ c
 
    # After this, c has value of b
    c = a ^ b ^ c
 
    # After this, a has value of c
    a = a ^ b ^ c
 
    return a , b , c
 
# Function that swaps n integers
def rearrangeArray(a, n):
 
    if (n % 2 == 0):
        for i in range (0, n - 1, 2):
             
            # Swap 2 variables without
            # using 3rd variable
            a[i], a[i + 1] = swapTwo(a[i], a[i + 1])
         
    else:
        for i in range(0, n - 3, 2):
             
            # Swap 2 variables without
            # using 3rd variable
            a[i], a[i + 1] = swapTwo(a[i], a[i + 1])
 
        # The last 3 elements will not form
        # pair if n is odd
        # Hence, swapp 3 variables without
        # using 4th variable
        a[n - 1], a[n - 2], a[n - 3] = swapThree(a[n - 1],
                                                 a[n - 2],
                                                 a[n - 3])
 
    # Print the array elements
    print1(a, n)
 
# Driver Code
if __name__ == "__main__":
 
    # Given array arr[]
    arr = [1, 2, 3, 4, 5 ]
    n = len(arr)
 
    # Function call
    rearrangeArray(arr, n)
 
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to print the array
static void print(int []a, int n)
{
     
    // Loop to iterate over the
    // elements of array
    for(int i = 0; i < n; i++)
        Console.Write(a[i] + " ");
}
 
// Function to swap two variables without
// using the third variable
static void swapTwo(ref int x, ref int y)
{
     
    // Store XOR of all in x
    x = x ^ y;
 
    // After this, y has value of x
    y = x ^ y;
 
    // After this, x has value of y
    x = x ^ y;
}
 
// Function to swap three variables
// without using fourth variable
static void swapThree(ref int a, ref int b,
                      ref int c)
{
     
    // Store XOR of all in a
    a = a ^ b ^ c;
 
    // After this, b has value of a
    b = a ^ b ^ c;
 
    // After this, c has value of b
    c = a ^ b ^ c;
 
    // After this, a has value of c
    a = a ^ b ^ c;
}
 
// Function that swaps n integers
static void rearrangeArray(int []a, int n)
{
    if (n % 2 == 0)
    {
        for(int i = 0; i < n - 1; i += 2)
        {
             
            // Swap 2 variables without
            // using 3rd variable
            swapTwo(ref a[i], ref a[i + 1]);
        }
    }
    else
    {
        for(int i = 0; i < n - 3; i += 2)
        {
             
            // Swap 2 variables without
            // using 3rd variable
            swapTwo(ref a[i], ref a[i + 1]);
        }
 
        // The last 3 elements will not form
        // pair if n is odd
        // Hence, swapp 3 variables without
        // using 4th variable
        swapThree(ref a[n - 1], ref a[n - 2],
                  ref a[n - 3]);
    }
 
    // Print the array elements
    print(a, n);
}
 
// Driver Code
public static void Main(string []s)
{
     
    // Given array arr[]
    int []arr = { 1, 2, 3, 4, 5 };
    int n = arr.Length;
 
    // Function call
    rearrangeArray(arr, n);
}
}
 
// This code is contributed by rutvik_56


Javascript


输出:
2 1 4 5 3

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

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