给定一个数组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 }不使用额外空间。
例子:
Input: arr[] = { 1, 3, 5, 2, 4, 6 }
Output: 1 2 3 4 5 6
Explanation:
The output contains the elements in the form of { a1, b1, a2, b2, a3, b3 }.
Input: arr[] = {1, 2, 3, -1, -2, -3, }
Output: 1 -1 2 -2 3 -3
基于分而治之的方法:如果数组的大小是 2 的幂,那么按照文章 Shuffle 2n integers in format {a1, b1, a2, b2, a3, b3, ……, an, bn} usingdivide和征服技术。
时间复杂度: O(N * log(N))
辅助空间: O(1)
替代方法:上述方法将通过递归划分数组使两半的长度为偶数来适用于所有可能的N值。请按照以下步骤解决问题:
- 定义一个递归函数,比如shuffle(start, end) 。
- 如果数组长度可被 4 整除,则计算数组的中点,例如mid = start + (end – start + 1) / 2。
- 否则, mid = start + (end – start + 1) / 2 – 1。
- 计算两个子数组的中点,比如mid1 = start + (mid – start)/2 ,而mid2 = 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) 。
- 最后,打印数组。
插图:
Consider an array arr[] = {a1, a2, a3, b1, b2, b3}:
- Split the array into two halves, both of even length, i.e. a1, a2 : a3, b1, b2, b3.
- Reverse the mid of first half to mid of 2nd half, i.e. a1, b1 : a3, a2, b2, b3.
- Now, reverse the mid of first half to mid of subarray [0, 5], a1, b1 : a3, a2, b2, b3.
- Now reverse mid of subarray [0, 5] to mid of 2nd half, a1, b1 : a2, a3, b2, b3.
- Recursively call for arrays {a1, b1}, and {a2, a3, b2, b3}.
- Now the array {a2, a3, b2, b3} modifies to {a2, b2, a3, b3} after applying the above operations.
- Now the arr[] modifies to {a1, b1, a2, b2, a3, b3}.
下面是上述方法的实现:
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)
# Print the 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
Javascript
1 2 3 4 5 6
时间复杂度:O(N * log(N))
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live