重新排列数组,使得 arr[i] >= arr[j] 如果 i 是偶数并且 arr[i]<=arr[j] 如果 i 是奇数并且 j < i
给定一个包含 n 个元素的数组。我们的任务是编写一个程序来重新排列数组,使得偶数位置的元素大于它之前的所有元素,奇数位置的元素小于它之前的所有元素。
例子:
Input : arr[] = {1, 2, 3, 4, 5, 6, 7}
Output : 4 5 3 6 2 7 1
Input : arr[] = {1, 2, 1, 4, 5, 6, 8, 8}
Output : 4 5 2 6 1 8 1 8
解决这个问题的思路是首先创建原始数组的辅助副本,并对复制的数组进行排序。现在具有 n 个元素的数组中偶数位置的总数将是 floor(n/2),剩余的是奇数位置的数量。现在使用排序数组以以下方式填充原始数组中的奇数和偶数位置:
- 奇数仓位总数为 n – floor(n/2)。从排序数组中的第 (n-floor(n/2)) 个位置开始,并将元素复制到排序数组的第一个位置。开始从这个位置向左遍历排序后的数组,并继续向右填充原始数组中的奇数位置。
- 从第 (n-floor(n/2)+1) 位置开始向右遍历排序数组,并从第 2 位置开始填充原始数组。
下面是上述想法的实现:
C++
// C++ program to rearrange the array
// as per the given condition
#include
using namespace std;
// function to rearrange the array
void rearrangeArr(int arr[], int n)
{
// total even positions
int evenPos = n / 2;
// total odd positions
int oddPos = n - evenPos;
int tempArr[n];
// copy original array in an
// auxiliary array
for (int i = 0; i < n; i++)
tempArr[i] = arr[i];
// sort the auxiliary array
sort(tempArr, tempArr + n);
int j = oddPos - 1;
// fill up odd position in original
// array
for (int i = 0; i < n; i += 2) {
arr[i] = tempArr[j];
j--;
}
j = oddPos;
// fill up even positions in original
// array
for (int i = 1; i < n; i += 2) {
arr[i] = tempArr[j];
j++;
}
// display array
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
// Driver code
int main()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
int size = sizeof(arr) / sizeof(arr[0]);
rearrangeArr(arr, size);
return 0;
}
Java
// Java program to rearrange the array
// as per the given condition
import java.util.*;
import java.lang.*;
public class GfG{
// function to rearrange the array
public static void rearrangeArr(int arr[],
int n)
{
// total even positions
int evenPos = n / 2;
// total odd positions
int oddPos = n - evenPos;
int[] tempArr = new int [n];
// copy original array in an
// auxiliary array
for (int i = 0; i < n; i++)
tempArr[i] = arr[i];
// sort the auxiliary array
Arrays.sort(tempArr);
int j = oddPos - 1;
// fill up odd position in
// original array
for (int i = 0; i < n; i += 2) {
arr[i] = tempArr[j];
j--;
}
j = oddPos;
// fill up even positions in
// original array
for (int i = 1; i < n; i += 2) {
arr[i] = tempArr[j];
j++;
}
// display array
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
// Driver function
public static void main(String argc[]){
int[] arr = new int []{ 1, 2, 3, 4, 5,
6, 7 };
int size = 7;
rearrangeArr(arr, size);
}
}
/* This code is contributed by Sagar Shukla */
Python3
# Python3 code to rearrange the array
# as per the given condition
import array as a
import numpy as np
# function to rearrange the array
def rearrangeArr(arr, n):
# total even positions
evenPos = int(n / 2)
# total odd positions
oddPos = n - evenPos
# initialising empty array in python
tempArr = np.empty(n, dtype = object)
# copy original array in an
# auxiliary array
for i in range(0, n):
tempArr[i] = arr[i]
# sort the auxiliary array
tempArr.sort()
j = oddPos - 1
# fill up odd position in original
# array
for i in range(0, n, 2):
arr[i] = tempArr[j]
j = j - 1
j = oddPos
# fill up even positions in original
# array
for i in range(1, n, 2):
arr[i] = tempArr[j]
j = j + 1
# display array
for i in range(0, n):
print (arr[i], end = ' ')
# Driver code
arr = a.array('i', [ 1, 2, 3, 4, 5, 6, 7 ])
rearrangeArr(arr, 7)
# This code is contributed by saloni1297
C#
// C# program to rearrange the array
// as per the given condition
using System;
public class GfG {
// Function to rearrange the array
public static void rearrangeArr(int []arr, int n)
{
// total even positions
int evenPos = n / 2;
// total odd positions
int oddPos = n - evenPos;
int[] tempArr = new int [n];
// copy original array in an
// auxiliary array
for (int i = 0; i < n; i++)
tempArr[i] = arr[i];
// sort the auxiliary array
Array.Sort(tempArr);
int j = oddPos - 1;
// Fill up odd position in
// original array
for (int i = 0; i < n; i += 2) {
arr[i] = tempArr[j];
j--;
}
j = oddPos;
// Fill up even positions in
// original array
for (int i = 1; i < n; i += 2) {
arr[i] = tempArr[j];
j++;
}
// display array
for (int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
}
// Driver Code
public static void Main()
{
int[] arr = new int []{ 1, 2, 3, 4, 5, 6, 7 };
int size = 7;
rearrangeArr(arr, size);
}
}
/* This code is contributed by vt_m */
PHP
Javascript
C++
#include
using namespace std;
int main(){
int n,i,j,p,q;
int a[]= {1, 2, 1, 4, 5, 6, 8, 8};
n=sizeof(a)/sizeof(a[0]);
int b[n];
for(i=0;i=0;i--){
if(i%2!=0){
a[i]=b[q];
q--;
}
else{
a[i]=b[p];
p++;
}
}
for(i=0;i
Java
import java.util.*;
class GFG{
public static void main(String[] args)
{
int n, i, j, p, q;
int a[] = {1, 2, 1, 4, 5, 6, 8, 8};
n = a.length;
int []b = new int[n];
for(i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
p = 0; q = n - 1;
for(i = n - 1; i >= 0; i--)
{
if(i % 2 != 0)
{
a[i] = b[q];
q--;
}
else{
a[i] = b[p];
p++;
}
}
for(i = 0; i < n; i++)
{
System.out.print(a[i]+" ");
}
}
}
// This code is contributed by gauravrajput1
Python3
if __name__ == '__main__':
#n, i, j, p, q;
a = [ 1, 2, 1, 4, 5, 6, 8, 8 ];
n = len(a);
b = [0]*n;
for i in range(n):
b[i] = a[i];
b.sort();
p = 0;
q = n - 1;
for i in range(n-1, -1,-1):
if (i % 2 != 0):
a[i] = b[q];
q -= 1;
else:
a[i] = b[p];
p += 1;
for i in range(n):
print(a[i], end=" ");
# This code is contributed by gauravrajput1
C#
using System;
public class GFG{
public static void Main(String[] args)
{
int n, i, j, p, q;
int []a = {1, 2, 1, 4, 5, 6, 8, 8};
n = a.Length;
int []b = new int[n];
for(i = 0; i < n; i++)
b[i] = a[i];
Array.Sort(b);
p = 0; q = n - 1;
for(i = n - 1; i >= 0; i--)
{
if(i % 2 != 0)
{
a[i] = b[q];
q--;
}
else{
a[i] = b[p];
p++;
}
}
for(i = 0; i < n; i++)
{
Console.Write(a[i]+" ");
}
}
}
// This code is contributed by gauravrajput1
Javascript
输出:
4 5 3 6 2 7 1
时间复杂度:O(n logn)
辅助空间:O(n)
另一种方法-
我们可以通过定义两个变量 p 和 q 来遍历数组并从 last 赋值。
如果甚至有索引,那么我们将给它最大值,否则给它最小值。
p = 0 和 q = 结束;
p 将继续,q 将减小。
C++
#include
using namespace std;
int main(){
int n,i,j,p,q;
int a[]= {1, 2, 1, 4, 5, 6, 8, 8};
n=sizeof(a)/sizeof(a[0]);
int b[n];
for(i=0;i=0;i--){
if(i%2!=0){
a[i]=b[q];
q--;
}
else{
a[i]=b[p];
p++;
}
}
for(i=0;i
Java
import java.util.*;
class GFG{
public static void main(String[] args)
{
int n, i, j, p, q;
int a[] = {1, 2, 1, 4, 5, 6, 8, 8};
n = a.length;
int []b = new int[n];
for(i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
p = 0; q = n - 1;
for(i = n - 1; i >= 0; i--)
{
if(i % 2 != 0)
{
a[i] = b[q];
q--;
}
else{
a[i] = b[p];
p++;
}
}
for(i = 0; i < n; i++)
{
System.out.print(a[i]+" ");
}
}
}
// This code is contributed by gauravrajput1
Python3
if __name__ == '__main__':
#n, i, j, p, q;
a = [ 1, 2, 1, 4, 5, 6, 8, 8 ];
n = len(a);
b = [0]*n;
for i in range(n):
b[i] = a[i];
b.sort();
p = 0;
q = n - 1;
for i in range(n-1, -1,-1):
if (i % 2 != 0):
a[i] = b[q];
q -= 1;
else:
a[i] = b[p];
p += 1;
for i in range(n):
print(a[i], end=" ");
# This code is contributed by gauravrajput1
C#
using System;
public class GFG{
public static void Main(String[] args)
{
int n, i, j, p, q;
int []a = {1, 2, 1, 4, 5, 6, 8, 8};
n = a.Length;
int []b = new int[n];
for(i = 0; i < n; i++)
b[i] = a[i];
Array.Sort(b);
p = 0; q = n - 1;
for(i = n - 1; i >= 0; i--)
{
if(i % 2 != 0)
{
a[i] = b[q];
q--;
}
else{
a[i] = b[p];
p++;
}
}
for(i = 0; i < n; i++)
{
Console.Write(a[i]+" ");
}
}
}
// This code is contributed by gauravrajput1
Javascript
这个算法将比前一个循环少 1 个。