给定整数数组arr [] ,任务是以类似于摆锤往复运动的方式布置它们,而无需使用任何额外的空间。
摆的安排:
- 整数列表中的最小元素必须位于数组的中心位置。
- 编号从小到大依次位于最小号的右边,下一个较大的号在最小号的左边,然后继续。
- 当达到更高的数字时,人像钟摆一样以往复的方式移到一侧。
例子:
Input: arr[] = {2, 3, 5, 1, 4}
Output: 5 3 1 2 4
The minimum element is 1, so it is moved to the middle.
The next higher element 2 is moved to the right of the
middle element while the next higher element 3 is
moved to the left of the middle element and
this process is continued.
Input: arr[] = {11, 2, 4, 55, 6, 8}
Output: 11 6 2 4 8 55
方法:本文讨论了一种使用辅助数组的方法。这是一种不占用额外空间的方法:
- 对给定的数组进行排序。
- 将所有奇数位置元素移到数组的右侧。
- 将元素从0反转到数组的(n-1)/ 2位置。
For example, let arr[] = {2, 3, 5, 1, 4}
Sorted array will be arr[] = {1, 2, 3, 4, 5}.
After moving all odd index position elements to the right,
arr[] = {1, 3, 5, 2, 4} (1 and 3 are the odd index positions)
After reversing elements from 0 to (n – 1) / 2,
arr[] = {5, 3, 1, 2, 4} which is the required array.
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to print the Pendulum
// arrangement of the given array
void pendulumArrangement(int arr[], int n)
{
// Sort the array
sort(arr, arr + n);
int odd, temp, in, pos;
// pos stores the index of
// the last element of the array
pos = n - 1;
// odd stores the last odd index in the array
if (n % 2 == 0)
odd = n - 1;
else
odd = n - 2;
// Move all odd index positioned
// elements to the right
while (odd > 0) {
temp = arr[odd];
in = odd;
// Shift the elements by one position
// from odd to pos
while (in != pos) {
arr[in] = arr[in + 1];
in++;
}
arr[in] = temp;
odd = odd - 2;
pos = pos - 1;
}
// Reverse the element from 0 to (n - 1) / 2
int start = 0, end = (n - 1) / 2;
for (; start < end; start++, end--) {
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
// Printing the pendulum arrangement
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
// Driver code
int main()
{
int arr[] = { 11, 2, 4, 55, 6, 8 };
int n = sizeof(arr) / sizeof(arr[0]);
pendulumArrangement(arr, n);
return 0;
}
Java
// Java implementation of the approach
import java.util.Arrays;
import java.io.*;
class GFG {
// Function to print the Pendulum
// arrangement of the given array
static void pendulumArrangement(int arr[], int n)
{
// Sort the array
// sort(arr, arr + n);
Arrays.sort(arr);
int odd, temp, in, pos;
// pos stores the index of
// the last element of the array
pos = n - 1;
// odd stores the last odd index in the array
if (n % 2 == 0)
odd = n - 1;
else
odd = n - 2;
// Move all odd index positioned
// elements to the right
while (odd > 0) {
temp = arr[odd];
in = odd;
// Shift the elements by one position
// from odd to pos
while (in != pos) {
arr[in] = arr[in + 1];
in++;
}
arr[in] = temp;
odd = odd - 2;
pos = pos - 1;
}
// Reverse the element from 0 to (n - 1) / 2
int start = 0, end = (n - 1) / 2;
for (; start < end; start++, end--) {
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
// Printing the pendulum arrangement
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 11, 2, 4, 55, 6, 8 };
int n = arr.length;
pendulumArrangement(arr, n);
}
}
// This code is contributed by akt_mit
Python3
# Python 3 implementation of the approach
# Function to print the Pendulum
# arrangement of the given array
def pendulumArrangement(arr, n):
# Sort the array
arr.sort(reverse = False)
# pos stores the index of
# the last element of the array
pos = n - 1
# odd stores the last odd index in the array
if (n % 2 == 0):
odd = n - 1
else:
odd = n - 2
# Move all odd index positioned
# elements to the right
while (odd > 0):
temp = arr[odd]
in1 = odd
# Shift the elements by one position
# from odd to pos
while (in1 != pos):
arr[in1] = arr[in1 + 1]
in1 += 1
arr[in1] = temp
odd = odd - 2
pos = pos - 1
# Reverse the element from 0 to (n - 1) / 2
start = 0
end = int((n - 1) / 2)
while(start < end):
temp = arr[start]
arr[start] = arr[end]
arr[end] = temp
start += 1
end -= 1
# Printing the pendulum arrangement
for i in range(n):
print(arr[i], end = " ")
# Driver code
if __name__ == '__main__':
arr = [11, 2, 4, 55, 6, 8]
n = len(arr)
pendulumArrangement(arr, n)
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to print the Pendulum
// arrangement of the given array
static void pendulumArrangement(int[] arr, int n)
{
// Sort the array
// sort(arr, arr + n);
Array.Sort(arr);
int odd, temp, p, pos;
// pos stores the index of
// the last element of the array
pos = n - 1;
// odd stores the last odd index in the array
if (n % 2 == 0)
odd = n - 1;
else
odd = n - 2;
// Move all odd index positioned
// elements to the right
while (odd > 0)
{
temp = arr[odd];
p = odd;
// Shift the elements by one position
// from odd to pos
while (p != pos)
{
arr[p] = arr[p + 1];
p++;
}
arr[p] = temp;
odd = odd - 2;
pos = pos - 1;
}
// Reverse the element from 0 to (n - 1) / 2
int start = 0, end = (n - 1) / 2;
for (; start < end; start++, end--)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
// Printing the pendulum arrangement
for (int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
}
// Driver code
public static void Main()
{
int[] arr = { 11, 2, 4, 55, 6, 8 };
int n = arr.Length;
pendulumArrangement(arr, n);
}
}
// This code is contributed by ChitraNayal
PHP
0)
{
$temp = $arr[$odd];
$in = $odd;
// Shift the elements by one position
// from odd to pos
while ($in != $pos)
{
$arr[$in] = $arr[$in + 1];
$in++;
}
$arr[$in] = $temp;
$odd = $odd - 2;
$pos = $pos - 1;
}
// Reverse the element from 0 to (n - 1) / 2
$start = 0;
$end = floor(($n - 1) / 2);
for (; $start < $end; $start++, $end--)
{
$temp = $arr[$start];
$arr[$start] = $arr[$end];
$arr[$end] = $temp;
}
// Printing the pendulum arrangement
for ($i = 0; $i < $n; $i++)
echo $arr[$i], " ";
}
// Driver code
$arr = array( 11, 2, 4, 55, 6, 8 );
$n = count($arr);
pendulumArrangement($arr, $n);
// This code is contributed by AnkitRai01
?>
11 6 2 4 8 55