📌  相关文章
📜  将2n个整数作为a1-b1-a2-b2-a3-b3-.. bn随机排列,而无需使用多余的空间|套装2

📅  最后修改于: 2021-04-22 06:44:30             🧑  作者: Mango

给定数组arr []2 * N个元素组成,形式为{a 1 ,a 2 ,…,a N ,b 1 ,b 2 ,…,b N} ,任务是将数组改组为{a 1 ,b 1 ,a 2 ,b 2 ,…,a n ,b 1 },而无需使用额外的空间。

例子

基于分而治之的方法:如果数组的大小为2的幂,则使用除法按照{a1,b1,a2,b2,a3,b3,……,an,bn}的格式洗牌2n整数和征服技术。
时间复杂度: O(N * log(N))
辅助空间: O(1)

替代方法:通过递归划分数组,使两半的长度均等,上述方法将适用于N的所有可能值。请按照以下步骤解决问题:

  • 定义一个递归函数,例如shuffle(start,end)
    • 如果将数组长度除以4,则计算数组的中点,例如mid =开始+(结束–开始+ 1)/ 2。
    • 否则, mid =开始+(结束-开始+ 1)/ 2-1。
    • 计算两个子数组的中点,例如mid1 = start +(mid – start)/ 2mid2 = mid +(end – mid + 1)/ 2。
    • 反转范围为[mid1,mid2],[mid1,mid-1][mid, mid2-1]的子阵列
    • 对子数组[start,mid – 1][mid,end]进行递归调用,分别是shuffle(start,mid-1)shuffle(mid,end)
  • 最后,打印数组。

插图:

下面是上述方法的实现:

C
// C program for the above approach
#include 
 
// Function to reverse the array from the
// position 'start' to position 'end'
void reverse(int arr[], int start, int end)
{
 
    // Stores mid of start and end
    int mid = (end - start + 1) / 2;
 
    // Traverse the array in
    // the range [start, end]
    for (int i = 0; i < mid; i++) {
 
        // Stores arr[start + i]
        int temp = arr[start + i];
 
        // Update arr[start + i]
        arr[start + i] = arr[end - i];
 
        // Update arr[end - i]
        arr[end - i] = temp;
    }
    return;
}
 
// Utility function to shuffle the given array
// in the of form {a1, b1, a2, b2, ....an, bn}
void shuffleArrayUtil(int arr[], int start, int end)
{
    int i;
 
    // Stores the length of the array
    int l = end - start + 1;
 
    // If length of the array is 2
    if (l == 2)
        return;
 
    // Stores mid of the { start, end }
    int mid = start + l / 2;
 
    // Divide array into two
    // halves of even length
    if (l % 4) {
 
        // Update mid
        mid -= 1;
    }
 
    // Calculate the mid-points of
    // both halves of the array
    int mid1 = start + (mid - start) / 2;
    int mid2 = mid + (end + 1 - mid) / 2;
 
    // Reverse the subarray made
    // from mid1 to mid2
    reverse(arr, mid1, mid2 - 1);
 
    // Reverse the subarray made
    // from mid1 to mid
    reverse(arr, mid1, mid - 1);
 
    // Reverse the subarray made
    // from mid to mid2
    reverse(arr, mid, mid2 - 1);
 
    // Recursively calls for both
    // the halves of the array
    shuffleArrayUtil(arr, start, mid - 1);
    shuffleArrayUtil(arr, mid, end);
}
 
// Function to shuffle the given array in
// the form of {a1, b1, a2, b2, ....an, bn}
void shuffleArray(int arr[], int N,
                  int start, int end)
{
 
    // Function Call
    shuffleArrayUtil(arr, start, end);
 
    // Print the modified array
    for (int i = 0; i < N; i++)
        printf("%d ", arr[i]);
}
 
// Driver Code
int main()
{
 
    // Given array
    int arr[] = { 1, 3, 5, 2, 4, 6 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Shuffles the given array to the
    // required permutation
    shuffleArray(arr, N, 0, N - 1);
 
    return 0;
}


Java
// Java program for the above approach
 
class GFG{
   
// Function to reverse the array from the
// position 'start' to position 'end'
static void reverse(int arr[], int start, int end)
{
 
    // Stores mid of start and end
    int mid = (end - start + 1) / 2;
 
    // Traverse the array in
    // the range [start, end]
    for (int i = 0; i < mid; i++)
    {
 
        // Stores arr[start + i]
        int temp = arr[start + i];
 
        // Update arr[start + i]
        arr[start + i] = arr[end - i];
 
        // Update arr[end - i]
        arr[end - i] = temp;
    }
    return;
}
 
// Utility function to shuffle the given array
// in the of form {a1, b1, a2, b2, ....an, bn}
static void shuffleArrayUtil(int arr[], int start, int end)
{
    int i;
 
    // Stores the length of the array
    int l = end - start + 1;
 
    // If length of the array is 2
    if (l == 2)
        return;
 
    // Stores mid of the { start, end }
    int mid = start + l / 2;
 
    // Divide array into two
    // halves of even length
    if (l % 4 > 0)
    {
 
        // Update mid
        mid -= 1;
    }
 
    // Calculate the mid-points of
    // both halves of the array
    int mid1 = start + (mid - start) / 2;
    int mid2 = mid + (end + 1 - mid) / 2;
 
    // Reverse the subarray made
    // from mid1 to mid2
    reverse(arr, mid1, mid2 - 1);
 
    // Reverse the subarray made
    // from mid1 to mid
    reverse(arr, mid1, mid - 1);
 
    // Reverse the subarray made
    // from mid to mid2
    reverse(arr, mid, mid2 - 1);
 
    // Recursively calls for both
    // the halves of the array
    shuffleArrayUtil(arr, start, mid - 1);
    shuffleArrayUtil(arr, mid, end);
}
 
// Function to shuffle the given array in
// the form of {a1, b1, a2, b2, ....an, bn}
static void shuffleArray(int arr[], int N,
                  int start, int end)
{
 
    // Function Call
    shuffleArrayUtil(arr, start, end);
 
    // Print the modified array
    for (int i = 0; i < N; i++)
        System.out.printf("%d ", arr[i]);
}
 
// Driver Code
public static void main(String[] args)
{
 
    // Given array
    int arr[] = { 1, 3, 5, 2, 4, 6 };
 
    // Size of the array
    int N = arr.length;
 
    // Shuffles the given array to the
    // required permutation
    shuffleArray(arr, N, 0, N - 1);
 
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to reverse the array from the
# position 'start' to position 'end'
def reverse(arr, start, end):
 
    # Stores mid of start and end
    mid = (end - start + 1) // 2
 
    # Traverse the array in
    # the range [start, end]
    for i in range(mid):
 
        # Stores arr[start + i]
        temp = arr[start + i]
 
        # Update arr[start + i]
        arr[start + i] = arr[end - i]
 
        # Update arr[end - i]
        arr[end - i] = temp
    return arr
 
# Utility function to shuffle the given array
# in the of form {a1, b1, a2, b2, ....an, bn}
def shuffleArrayUtil(arr, start, end):
    i = 0
 
    # Stores the length of the array
    l = end - start + 1
 
    # If length of the array is 2
    if (l == 2):
        return
 
    # Stores mid of the { start, end }
    mid = start + l // 2
 
    # Divide array into two
    # halves of even length
    if (l % 4):
 
        # Update mid
        mid -= 1
 
    # Calculate the mid-points of
    # both halves of the array
    mid1 = start + (mid - start) // 2
    mid2 = mid + (end + 1 - mid) // 2
 
    # Reverse the subarray made
    # from mid1 to mid2
    arr = reverse(arr, mid1, mid2 - 1)
 
    # Reverse the subarray made
    # from mid1 to mid
    arr = reverse(arr, mid1, mid - 1)
 
    # Reverse the subarray made
    # from mid to mid2
    arr = reverse(arr, mid, mid2 - 1)
 
    # Recursively calls for both
    # the halves of the array
    shuffleArrayUtil(arr, start, mid - 1)
    shuffleArrayUtil(arr, mid, end)
 
# Function to shuffle the given array in
# the form of {a1, b1, a2, b2, ....an, bn}
def shuffleArray(arr, N, start, end):
 
    # Function Call
    shuffleArrayUtil(arr, start, end)
 
    # Prthe modified array
    for i in arr:
        print(i, end=" ")
 
# Driver Code
if __name__ == '__main__':
 
    # Given array
    arr = [1, 3, 5, 2, 4, 6]
 
    # Size of the array
    N = len(arr)
 
    # Shuffles the given array to the
    # required permutation
    shuffleArray(arr, N, 0, N - 1)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
public class GFG{
 
// Function to reverse the array from the
// position 'start' to position 'end'
static void reverse(int[] arr, int start, int end)
{
 
    // Stores mid of start and end
    int mid = (end - start + 1) / 2;
 
    // Traverse the array in
    // the range [start, end]
    for (int i = 0; i < mid; i++)
    {
 
        // Stores arr[start + i]
        int temp = arr[start + i];
 
        // Update arr[start + i]
        arr[start + i] = arr[end - i];
 
        // Update arr[end - i]
        arr[end - i] = temp;
    }
    return;
}
 
// Utility function to shuffle the given array
// in the of form {a1, b1, a2, b2, ....an, bn}
static void shuffleArrayUtil(int[] arr, int start, int end)
{
   
    // Stores the length of the array
    int l = end - start + 1;
 
    // If length of the array is 2
    if (l == 2)
        return;
 
    // Stores mid of the { start, end }
    int mid = start + l / 2;
 
    // Divide array into two
    // halves of even length
    if (l % 4 > 0)
    {
 
        // Update mid
        mid -= 1;
    }
 
    // Calculate the mid-points of
    // both halves of the array
    int mid1 = start + (mid - start) / 2;
    int mid2 = mid + (end + 1 - mid) / 2;
 
    // Reverse the subarray made
    // from mid1 to mid2
    reverse(arr, mid1, mid2 - 1);
 
    // Reverse the subarray made
    // from mid1 to mid
    reverse(arr, mid1, mid - 1);
 
    // Reverse the subarray made
    // from mid to mid2
    reverse(arr, mid, mid2 - 1);
 
    // Recursively calls for both
    // the halves of the array
    shuffleArrayUtil(arr, start, mid - 1);
    shuffleArrayUtil(arr, mid, end);
}
 
// Function to shuffle the given array in
// the form of {a1, b1, a2, b2, ....an, bn}
static void shuffleArray(int[] arr, int N,
                  int start, int end)
{
 
    // Function Call
    shuffleArrayUtil(arr, start, end);
 
    // Print the modified array
    for (int i = 0; i < N; i++)
        Console.Write(arr[i] + " ");
}
 
// Driver Code
static public void Main ()
{
 
    // Given array
    int[] arr = { 1, 3, 5, 2, 4, 6 };
 
    // Size of the array
    int N = arr.Length;
 
    // Shuffles the given array to the
    // required permutation
    shuffleArray(arr, N, 0, N - 1);
 
}
}
 
// This code is contributed by Dharanendra L V


输出:
1 2 3 4 5 6

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