给定一个包含2n 个元素的数组,格式为 { a1, a2, a3, a4, ….., an, b1, b2, b3, b4, …., bn }。任务是在不使用额外空间的情况下将数组洗牌为 {a1, b1, a2, b2, a3, b3, ……, an, bn }。
例子:
Input : arr[] = { 1, 2, 9, 15 }
Output : 1 9 2 15
Input : arr[] = { 1, 2, 3, 4, 5, 6 }
Output : 1 4 2 5 3 6
方法一:蛮力
蛮力解决方案涉及两个嵌套循环,以将数组后半部分中的元素向左旋转。第一个循环运行 n 次以覆盖数组后半部分中的所有元素。第二个循环将元素向左旋转。请注意,第二个循环中的开始索引取决于我们正在旋转的元素,结束索引取决于我们需要向左移动多少个位置。
下面是这种方法的实现:
C++
// C++ Naive program to shuffle an array of size 2n
#include
using namespace std;
// function to shuffle an array of size 2n
void shuffleArray(int a[], int n)
{
// Rotate the element to the left
for (int i = 0, q = 1, k = n; i < n; i++, k++, q++)
for (int j = k; j > i + q; j--)
swap(a[j - 1], a[j]);
}
// Driven Program
int main()
{
int a[] = { 1, 3, 5, 7, 2, 4, 6, 8 };
int n = sizeof(a) / sizeof(a[0]);
shuffleArray(a, n / 2);
for (int i = 0; i < n; i++)
cout << a[i] << " ";
return 0;
}
Java
// Java Naive program to shuffle an array of size 2n
import java.util.Arrays;
public class GFG {
// method to shuffle an array of size 2n
static void shuffleArray(int a[], int n)
{
// Rotate the element to the left
for (int i = 0, q = 1, k = n; i < n; i++, k++, q++)
for (int j = k; j > i + q; j--) {
// swap a[j-1], a[j]
int temp = a[j - 1];
a[j - 1] = a[j];
a[j] = temp;
}
}
// Driver Method
public static void main(String[] args)
{
int a[] = { 1, 3, 5, 7, 2, 4, 6, 8 };
shuffleArray(a, a.length / 2);
System.out.println(Arrays.toString(a));
}
}
Python3
# Python3 Naive program to
# shuffle an array of size 2n
# Function to shuffle an array of size 2n
def shuffleArray(a, n):
# Rotate the element to the left
i, q, k = 0, 1, n
while(i < n):
j = k
while(j > i + q):
a[j - 1], a[j] = a[j], a[j - 1]
j -= 1
i += 1
k += 1
q += 1
# Driver Code
a = [1, 3, 5, 7, 2, 4, 6, 8]
n = len(a)
shuffleArray(a, int(n / 2))
for i in range(0, n):
print(a[i], end = " ")
# This code is contributed by Smitha Dinesh Semwal.
C#
// C# Naive program to shuffle an
// array of size 2n
using System;
class GFG {
// method to shuffle an array of size 2n
static void shuffleArray(int[] a, int n)
{
// Rotate the element to the left
for (int i = 0, q = 1, k = n;
i < n; i++, k++, q++)
for (int j = k; j > i + q; j--) {
// swap a[j-1], a[j]
int temp = a[j - 1];
a[j - 1] = a[j];
a[j] = temp;
}
}
// Driver Code
public static void Main()
{
int[] a = { 1, 3, 5, 7, 2, 4, 6, 8 };
shuffleArray(a, a.Length / 2);
for (int i = 0; i < a.Length; i++)
Console.Write(a[i] + " ");
}
}
// This code is contributed
// by ChitraNayal
Javascript
C++
// C++ Effective program to shuffle an array of size 2n
#include
using namespace std;
// function to shuffle an array of size 2n
void shufleArray(int a[], int f, int l)
{
if (f > l) {
return;
}
// If only 2 element, return
if (l - f == 1)
return;
// finding mid to divide the array
int mid = (f + l) / 2;
// using temp for swapping first half of second array
int temp = mid + 1;
// mmid is use for swapping second half for first array
int mmid = (f + mid) / 2;
// Swapping the element
for (int i = mmid + 1; i <= mid; i++)
swap(a[i], a[temp++]);
// Recursively doing for first half and second half
shufleArray(a, f, mid);
shufleArray(a, mid + 1, l);
}
// Driven Program
int main()
{
int a[] = { 1, 3, 5, 7, 2, 4, 6, 8 };
int n = sizeof(a) / sizeof(a[0]);
shufleArray(a, 0, n - 1);
for (int i = 0; i < n; i++)
cout << a[i] << " ";
return 0;
}
Java
// Java Effective program to shuffle an array of size 2n
import java.util.Arrays;
public class GFG {
// method to shuffle an array of size 2n
static void shufleArray(int a[], int f, int l)
{
if (f > l)
return;
// If only 2 element, return
if (l - f == 1)
return;
// finding mid to divide the array
int mid = (f + l) / 2;
// using temp for swapping first half of second array
int temp = mid + 1;
// mmid is use for swapping second half for first array
int mmid = (f + mid) / 2;
// Swapping the element
for (int i = mmid + 1; i <= mid; i++) {
// swap a[i], a[temp++]
int temp1 = a[i];
a[i] = a[temp];
a[temp++] = temp1;
}
// Recursively doing for first half and second half
shufleArray(a, f, mid);
shufleArray(a, mid + 1, l);
}
// Driver Method
public static void main(String[] args)
{
int a[] = { 1, 3, 5, 7, 2, 4, 6, 8 };
shufleArray(a, 0, a.length - 1);
System.out.println(Arrays.toString(a));
}
}
Python3
# Python3 effective program to
# shuffle an array of size 2n
# Function to shuffle an array of size 2n
def shufleArray(a, f, l):
if (f > l):
return
# If only 2 element, return
if (l - f == 1):
return
# Finding mid to divide the array
mid = int((f + l) / 2)
# Using temp for swapping first
# half of the second array
temp = mid + 1
# Mid is use for swapping second
# half for first array
mmid = int((f + mid) / 2)
# Swapping the element
for i in range(mmid + 1, mid + 1):
(a[i], a[temp]) = (a[temp], a[i])
temp += 1
# Recursively doing for first
# half and second half
shufleArray(a, f, mid)
shufleArray(a, mid + 1, l)
# Driver Code
a = [1, 3, 5, 7, 2, 4, 6, 8]
n = len(a)
shufleArray(a, 0, n - 1)
for i in range(0, n):
print(a[i], end = " ")
# This code is contributed by Smitha Dinesh Semwal
C#
// C# program program to merge two
// sorted arrays with O(1) extra space.
using System;
// method to shuffle an array of size 2n
public class GFG {
// method to shuffle an array of size 2n
static void shufleArray(int[] a, int f, int l)
{
if (f > l)
return;
// If only 2 element, return
if (l - f == 1)
return;
// finding mid to divide the array
int mid = (f + l) / 2;
// using temp for swapping first half of second array
int temp = mid + 1;
// mmid is use for swapping second half for first array
int mmid = (f + mid) / 2;
// Swapping the element
for (int i = mmid + 1; i <= mid; i++) {
// swap a[i], a[temp++]
int temp1 = a[i];
a[i] = a[temp];
a[temp++] = temp1;
}
// Recursively doing for first half and second half
shufleArray(a, f, mid);
shufleArray(a, mid + 1, l);
}
// Driver Method
public static void Main()
{
int[] a = { 1, 3, 5, 7, 2, 4, 6, 8 };
shufleArray(a, 0, a.Length - 1);
for (int i = 0; i < a.Length; i++)
Console.Write(a[i] + " ");
}
}
/*This code is contributed by 29AjayKumar*/
Javascript
输出:
1 2 3 4 5 6 7 8
时间复杂度: O(n 2 )
方法二:(分而治之)
这个想法是使用分而治之的技术。将给定的数组分成两半(比如 arr1[] 和 arr2[]),并将 arr1[] 的后半元素与 arr2[] 的前半元素交换。对 arr1 和 arr2 递归执行此操作。
让我们用一个例子来解释。
- 设数组为 a1, a2, a3, a4, b1, b2, b3, b4
- 将数组分成两半: a1, a2, a3, a4 : b1, b2, b3, b4
- 围绕中心交换元素:将a3、a4与b1、b2对应交换。
你得到:a1、a2、b1、b2、a3、a4、b3、b4 - 递归地将 a1, a2, b1, b2 溢出到 a1, a2 : b1, b2
然后将 a3, a4, b3, b4 拆分为 a3, a4 : b3, b4。 - 为我们得到的每个子数组交换中心周围的元素:
a1、b1、a2、b2 和 a3、b3、a4、b4。
注意:此解决方案仅处理 n = 2 i 且i = 0, 1, 2, …等的情况。
下面是这种方法的实现:
C++
// C++ Effective program to shuffle an array of size 2n
#include
using namespace std;
// function to shuffle an array of size 2n
void shufleArray(int a[], int f, int l)
{
if (f > l) {
return;
}
// If only 2 element, return
if (l - f == 1)
return;
// finding mid to divide the array
int mid = (f + l) / 2;
// using temp for swapping first half of second array
int temp = mid + 1;
// mmid is use for swapping second half for first array
int mmid = (f + mid) / 2;
// Swapping the element
for (int i = mmid + 1; i <= mid; i++)
swap(a[i], a[temp++]);
// Recursively doing for first half and second half
shufleArray(a, f, mid);
shufleArray(a, mid + 1, l);
}
// Driven Program
int main()
{
int a[] = { 1, 3, 5, 7, 2, 4, 6, 8 };
int n = sizeof(a) / sizeof(a[0]);
shufleArray(a, 0, n - 1);
for (int i = 0; i < n; i++)
cout << a[i] << " ";
return 0;
}
Java
// Java Effective program to shuffle an array of size 2n
import java.util.Arrays;
public class GFG {
// method to shuffle an array of size 2n
static void shufleArray(int a[], int f, int l)
{
if (f > l)
return;
// If only 2 element, return
if (l - f == 1)
return;
// finding mid to divide the array
int mid = (f + l) / 2;
// using temp for swapping first half of second array
int temp = mid + 1;
// mmid is use for swapping second half for first array
int mmid = (f + mid) / 2;
// Swapping the element
for (int i = mmid + 1; i <= mid; i++) {
// swap a[i], a[temp++]
int temp1 = a[i];
a[i] = a[temp];
a[temp++] = temp1;
}
// Recursively doing for first half and second half
shufleArray(a, f, mid);
shufleArray(a, mid + 1, l);
}
// Driver Method
public static void main(String[] args)
{
int a[] = { 1, 3, 5, 7, 2, 4, 6, 8 };
shufleArray(a, 0, a.length - 1);
System.out.println(Arrays.toString(a));
}
}
蟒蛇3
# Python3 effective program to
# shuffle an array of size 2n
# Function to shuffle an array of size 2n
def shufleArray(a, f, l):
if (f > l):
return
# If only 2 element, return
if (l - f == 1):
return
# Finding mid to divide the array
mid = int((f + l) / 2)
# Using temp for swapping first
# half of the second array
temp = mid + 1
# Mid is use for swapping second
# half for first array
mmid = int((f + mid) / 2)
# Swapping the element
for i in range(mmid + 1, mid + 1):
(a[i], a[temp]) = (a[temp], a[i])
temp += 1
# Recursively doing for first
# half and second half
shufleArray(a, f, mid)
shufleArray(a, mid + 1, l)
# Driver Code
a = [1, 3, 5, 7, 2, 4, 6, 8]
n = len(a)
shufleArray(a, 0, n - 1)
for i in range(0, n):
print(a[i], end = " ")
# This code is contributed by Smitha Dinesh Semwal
C#
// C# program program to merge two
// sorted arrays with O(1) extra space.
using System;
// method to shuffle an array of size 2n
public class GFG {
// method to shuffle an array of size 2n
static void shufleArray(int[] a, int f, int l)
{
if (f > l)
return;
// If only 2 element, return
if (l - f == 1)
return;
// finding mid to divide the array
int mid = (f + l) / 2;
// using temp for swapping first half of second array
int temp = mid + 1;
// mmid is use for swapping second half for first array
int mmid = (f + mid) / 2;
// Swapping the element
for (int i = mmid + 1; i <= mid; i++) {
// swap a[i], a[temp++]
int temp1 = a[i];
a[i] = a[temp];
a[temp++] = temp1;
}
// Recursively doing for first half and second half
shufleArray(a, f, mid);
shufleArray(a, mid + 1, l);
}
// Driver Method
public static void Main()
{
int[] a = { 1, 3, 5, 7, 2, 4, 6, 8 };
shufleArray(a, 0, a.Length - 1);
for (int i = 0; i < a.Length; i++)
Console.Write(a[i] + " ");
}
}
/*This code is contributed by 29AjayKumar*/
Javascript
输出:
1 2 3 4 5 6 7 8
时间复杂度: O(n log n)
线性时间解
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。