将数组转换为 Zig-Zag 时尚
给定一个DISTINCT元素数组,在 O(n) 时间内以之字形方式重新排列数组的元素。转换后的数组的格式应为a < b > c < d > e < f 。
示例:
Input: arr[] = {4, 3, 7, 8, 6, 2, 1}
Output: arr[] = {3, 7, 4, 8, 2, 6, 1}
Input: arr[] = {1, 4, 3, 2}
Output: arr[] = {1, 4, 2, 3}
一个简单的解决方案是首先对数组进行排序。排序后,排除第一个元素,成对交换剩余元素。 (即保持 arr[0] 不变,交换 arr[1] 和 arr[2],交换 arr[3] 和 arr[4],等等)。
时间复杂度:O(N log N),因为我们需要先对数组进行排序。
我们可以使用有效的方法在O(n)时间内进行转换。这个想法是使用经过修改的冒泡排序的一次。
- 维护一个标志来表示我们当前需要的顺序(即 < 或 >)。
- 如果当前的两个元素不是该顺序,则交换这些元素,否则不交换。
让我们看看使用三个连续元素 A、B、C 的主要逻辑。
假设我们当前正在处理 B 和 C 并且当前关系是 '<',但是我们有 B > C。由于当前关系是 '<' 先前的关系必须是 '>' 即,A 必须大于 B。所以,关系是 A > B 和 B > C。我们可以推导出 A > C。所以如果我们交换 B 和 C,那么关系是 A > C 和 C < B。最后我们得到所需的顺序ACB
请参阅此以获取更多说明。
下图是上述方法的试运行:
以下是上述方法的实现:
C++
// C++ program to sort an array in Zig-Zag form
#include
using namespace std;
// Program for zig-zag conversion of array
void zigZag(int arr[], int n)
{
// Flag true indicates relation "<" is expected,
// else ">" is expected. The first expected relation
// is "<"
bool flag = true;
for (int i=0; i<=n-2; i++)
{
if (flag) /* "<" relation expected */
{
/* If we have a situation like A > B > C,
we get A > B < C by swapping B and C */
if (arr[i] > arr[i+1])
swap(arr[i], arr[i+1]);
}
else /* ">" relation expected */
{
/* If we have a situation like A < B < C,
we get A < C > B by swapping B and C */
if (arr[i] < arr[i+1])
swap(arr[i], arr[i+1]);
}
flag = !flag; /* flip flag */
}
}
// Driver program
int main()
{
int arr[] = {4, 3, 7, 8, 6, 2, 1};
int n = sizeof(arr)/sizeof(arr[0]);
zigZag(arr, n);
for (int i=0; i
Java
// Java program to sort an array in Zig-Zag form
import java.util.Arrays;
class Test
{
static int arr[] = new int[]{4, 3, 7, 8, 6, 2, 1};
// Method for zig-zag conversion of array
static void zigZag()
{
// Flag true indicates relation "<" is expected,
// else ">" is expected. The first expected relation
// is "<"
boolean flag = true;
int temp =0;
for (int i=0; i<=arr.length-2; i++)
{
if (flag) /* "<" relation expected */
{
/* If we have a situation like A > B > C,
we get A > B < C by swapping B and C */
if (arr[i] > arr[i+1])
{
// swap
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
else /* ">" relation expected */
{
/* If we have a situation like A < B < C,
we get A < C > B by swapping B and C */
if (arr[i] < arr[i+1])
{
// swap
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
flag = !flag; /* flip flag */
}
}
// Driver method to test the above function
public static void main(String[] args)
{
zigZag();
System.out.println(Arrays.toString(arr));
}
}
Python
# Python program to sort an array in Zig-Zag form
# Program for zig-zag conversion of array
def zigZag(arr, n):
# Flag true indicates relation "<" is expected,
# else ">" is expected. The first expected relation
# is "<"
flag = True
for i in range(n-1):
# "<" relation expected
if flag is True:
# If we have a situation like A > B > C,
# we get A > B < C
# by swapping B and C
if arr[i] > arr[i+1]:
arr[i],arr[i+1] = arr[i+1],arr[i]
# ">" relation expected
else:
# If we have a situation like A < B < C,
# we get A < C > B
# by swapping B and C
if arr[i] < arr[i+1]:
arr[i],arr[i+1] = arr[i+1],arr[i]
flag = bool(1 - flag)
print(arr)
# Driver program
arr = [4, 3, 7, 8, 6, 2, 1]
n = len(arr)
zigZag(arr, n)
# This code is contributed by Pratik Chhajer
C#
// C# program to sort an array in Zig-Zag form
using System;
class GFG{
static int []arr = new int[]{ 4, 3, 7, 8, 6, 2, 1 };
// Method for zig-zag conversion of array
static void zigZag()
{
// Flag true indicates relation "<"
// is expected, else ">" is expected.
// The first expected relation
// is "<"
bool flag = true;
int temp = 0;
for(int i = 0; i <= arr.Length - 2; i++)
{
// "<" relation expected
if (flag)
{
// If we have a situation like A > B > C,
// we get A > B < C by swapping B and C
if (arr[i] > arr[i+1])
{
// Swap
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
// ">" relation expected
else
{
// If we have a situation like A < B < C,
// we get A < C > B by swapping B and C
if (arr[i] < arr[i + 1])
{
// Swap
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
// Flip flag
flag = !flag;
}
}
// Driver code
public static void Main(String[] args)
{
zigZag();
foreach(int i in arr)
Console.Write(i + " ");
}
}
// This code is contributed by amal kumar choubey
Javascript
输出:
3 7 4 8 2 6 1
时间复杂度: O(n)
辅助空间: O(1)