📌  相关文章
📜  通过将中间元素交替移动到开始和结束来重新排列数组

📅  最后修改于: 2022-05-13 01:56:08.471000             🧑  作者: Mango

通过将中间元素交替移动到开始和结束来重新排列数组

给定一个数组,任务是将中间元素交替移动到数组的开头和结尾,直到中间元素等于原始数组的第一个元素。

朴素方法:将数组的中间元素交替移动到数组的开头和结尾。
如果 c 为偶数,则取中间元素并将其移至数组的开头;如果 c 为奇数,则将其移至数组的末尾。当中间元素等于原始数组的第一个元素时终止循环。

以下是上述方法的实现:

C++
// C++ program for above approach
#include 
using namespace std;
 
// Function for shifting middle element.
void AlternateShift(vector& arr, int x)
{
 
    // get middle index
    int mid = arr.size() / 2;
 
    // initialize c to 0
    int c = 0;
   
    // Shift middle element
    // till its value not equals to x.
    while (arr[mid] != x) {
 
        // pop middle element
 
        int z = arr[mid];
        arr.erase(arr.begin() + mid);
 
        // if c is even then insert z
        // at start of the array
        if (c % 2 == 0)
            arr.insert(arr.begin() + 0, z);
 
        // if c is odd then insert z
        // at end of the array
        else
            arr.push_back(z);
 
        // increment count c
        c += 1;
    }
}
 
int main()
{
    vector Arr = { 2, 8, 5, 9, 10 };
 
    // initialize a to zero index array value
    int a = Arr[0];
 
    // call AlternateShift function
    AlternateShift(Arr, a);
 
    // print the changed array Unpacking array
    for (int i = 0; i < Arr.size(); ++i) {
        cout << Arr[i] << " ";
    }
 
    return 0;
}
 
    // This code is contributed by rakeshsahni


Python3
# Function for shifting middle element.
def AlternateShift(arr, x):
 
    # get middle index
    mid = len(arr) // 2
 
    # initialize c to 0
    c = 0
 
    # Shift middle element
    # till its value not equals to x.
    while arr[mid] != x:
 
        # pop middle element
        z = arr.pop(mid)
 
        # if c is even then insert z
        # at start of the array
        if c % 2 == 0:
            arr.insert(0, z)
 
        # if c is odd then insert z
        # at end of the array
        else:
            arr.append(z)
 
        # increment count c
        c += 1
 
 
Arr = [2, 8, 5, 9, 10]
 
# initialize a to zero index array value
a = Arr[0]
 
# call AlternateShift function
AlternateShift(Arr, a)
 
# print the changed array Unpacking array
print(*Arr)


Javascript


C++
// C++ Program of the above approach
#include 
using namespace std;
 
// Function to to shift the middle
// element to the start and end of
// the array alternatively, till
// the middle element becomes equal to
// the first element of the original Array
int* rearrange(int* ar, int n)
{
    // creating the array to store
    // rearranged value
    int* br = new int[n];
 
    // initialising pos to last index
    int pos = n - 1;
 
    // if n is odd then we will
    // transverse the array
    // from second last element
    if (n % 2 != 0)
        pos = pos - 1;
 
    // storing index of middle element
    int mid = n / 2;
 
    // index variable for rearranged array
    int c = 0;
 
    // transversing the array from
    // the pos to mid index
    // and storing it in br[] array
    for (; pos >= mid; pos--)
        br = ar[pos];
 
    // storing the first element as
    // mid value
    br = ar[0];
 
    // if n is odd then store
    // the last value in br[] the
    // transverse till 1st index
    if (n % 2 != 0)
        br = ar[n - 1];
 
    // storing the first element of
    // array as mid value
    for (; pos >= 1; pos--)
        br = ar[pos];
 
    // returning br[] array
    return br;
}
// Driver Code
int main()
{
    int ar[] = { 2, 8, 5, 9, 10 };
    int n = sizeof(ar) / sizeof(ar[0]);
 
    // Function Call
    int* res = rearrange(ar, n);
 
    // Print answer
    for (int i = 0; i < n; i++)
        cout << res[i] << " ";
}


Java
// Java Program of the above approach
import java.util.*;
 
class GFG
{
 
// Function to to shift the middle
// element to the start and end of
// the array alternatively, till
// the middle element becomes equal to
// the first element of the original Array
static int[] rearrange(int[] ar, int n)
{
   
    // creating the array to store
    // rearranged value
    int[] br = new int[n];
 
    // initialising pos to last index
    int pos = n - 1;
 
    // if n is odd then we will
    // transverse the array
    // from second last element
    if (n % 2 != 0)
        pos = pos - 1;
 
    // storing index of middle element
    int mid = n / 2;
 
    // index variable for rearranged array
    int c = 0;
 
    // transversing the array from
    // the pos to mid index
    // and storing it in br[] array
    for (; pos >= mid; pos--)
        br = ar[pos];
 
    // storing the first element as
    // mid value
    br = ar[0];
 
    // if n is odd then store
    // the last value in br[] the
    // transverse till 1st index
    if (n % 2 != 0)
        br = ar[n - 1];
 
    // storing the first element of
    // array as mid value
    for (; pos >= 1; pos--)
        br = ar[pos];
 
    // returning br[] array
    return br;
}
   
// Driver Code
public static void main(String[] args)
{
    int ar[] = { 2, 8, 5, 9, 10 };
    int n = ar.length;
 
    // Function Call
    int[] res = rearrange(ar, n);
 
    // Print answer
    for (int i = 0; i < n; i++)
        System.out.print(res[i]+ " ");
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python 3 Program of the above approach
 
# Function to to shift the middle
# element to the start and end of
# the array alternatively, till
# the middle element becomes equal to
# the first element of the original Array
def rearrange(ar, n):
    # creating the array to store
    # rearranged value
    br = [0 for i in range(n)]
 
    # initialising pos to last index
    pos = n - 1
 
    # if n is odd then we will
    # transverse the array
    # from second last element
    if (n % 2 != 0):
        pos = pos - 1
 
    # storing index of middle element
    mid = n // 2
 
    # index variable for rearranged array
    c = 0
 
    # transversing the array from
    # the pos to mid index
    # and storing it in br[] array
    while(pos >= mid):
        br = ar[pos]
        c += 1
        pos -= 1
 
    # storing the first element as
    # mid value
    br = ar[0]
    c += 1
 
    # if n is odd then store
    # the last value in br[] the
    # transverse till 1st index
    if (n % 2 != 0):
        br = ar[n - 1]
        c += 1
 
    # storing the first element of
    # array as mid value
    while(pos >= 1):
        br = ar[pos]
        c += 1
        pos -= 1
 
    # returning br[] array
    return br
 
# Driver Code
if __name__ == '__main__':
    ar = [2, 8, 5, 9, 10]
    n = len(ar)
 
    # Function Call
    res = rearrange(ar, n)
 
    # Print answer
    for i in range(n):
        print(res[i],end = " ")
         
        # This code is contributed by ipg2016107.


C#
// C# Program of the above approach
using System;
 
public class GFG
{
 
// Function to to shift the middle
// element to the start and end of
// the array alternatively, till
// the middle element becomes equal to
// the first element of the original Array
static int[] rearrange(int[] ar, int n)
{
   
    // creating the array to store
    // rearranged value
    int[] br = new int[n];
 
    // initialising pos to last index
    int pos = n - 1;
 
    // if n is odd then we will
    // transverse the array
    // from second last element
    if (n % 2 != 0)
        pos = pos - 1;
 
    // storing index of middle element
    int mid = n / 2;
 
    // index variable for rearranged array
    int c = 0;
 
    // transversing the array from
    // the pos to mid index
    // and storing it in br[] array
    for (; pos >= mid; pos--)
        br = ar[pos];
 
    // storing the first element as
    // mid value
    br = ar[0];
 
    // if n is odd then store
    // the last value in br[] the
    // transverse till 1st index
    if (n % 2 != 0)
        br = ar[n - 1];
 
    // storing the first element of
    // array as mid value
    for (; pos >= 1; pos--)
        br = ar[pos];
 
    // returning br[] array
    return br;
}
   
// Driver Code
public static void Main(String[] args)
{
    int []ar = { 2, 8, 5, 9, 10 };
    int n = ar.Length;
 
    // Function Call
    int[] res = rearrange(ar, n);
 
    // Print answer
    for (int i = 0; i < n; i++)
        Console.Write(res[i]+ " ");
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出
9 5 2 10 8

时间复杂度: O(n 2 )
辅助空间: O(1)

高效方法:交替移位也是阵列半反转的情况。如果 n 为偶数,则首先从 last 到 mid 取一个元素,或者从 last 到 mid 取一个元素并插入新数组 br[],然后将第一个元素插入 br[]。然后将元素从 mid-1 插入到索引 1 并插入到 br[]。所以它将以半反转顺序返回数组。

算法 :

下面是上述算法的实现:

C++

// C++ Program of the above approach
#include 
using namespace std;
 
// Function to to shift the middle
// element to the start and end of
// the array alternatively, till
// the middle element becomes equal to
// the first element of the original Array
int* rearrange(int* ar, int n)
{
    // creating the array to store
    // rearranged value
    int* br = new int[n];
 
    // initialising pos to last index
    int pos = n - 1;
 
    // if n is odd then we will
    // transverse the array
    // from second last element
    if (n % 2 != 0)
        pos = pos - 1;
 
    // storing index of middle element
    int mid = n / 2;
 
    // index variable for rearranged array
    int c = 0;
 
    // transversing the array from
    // the pos to mid index
    // and storing it in br[] array
    for (; pos >= mid; pos--)
        br = ar[pos];
 
    // storing the first element as
    // mid value
    br = ar[0];
 
    // if n is odd then store
    // the last value in br[] the
    // transverse till 1st index
    if (n % 2 != 0)
        br = ar[n - 1];
 
    // storing the first element of
    // array as mid value
    for (; pos >= 1; pos--)
        br = ar[pos];
 
    // returning br[] array
    return br;
}
// Driver Code
int main()
{
    int ar[] = { 2, 8, 5, 9, 10 };
    int n = sizeof(ar) / sizeof(ar[0]);
 
    // Function Call
    int* res = rearrange(ar, n);
 
    // Print answer
    for (int i = 0; i < n; i++)
        cout << res[i] << " ";
}

Java

// Java Program of the above approach
import java.util.*;
 
class GFG
{
 
// Function to to shift the middle
// element to the start and end of
// the array alternatively, till
// the middle element becomes equal to
// the first element of the original Array
static int[] rearrange(int[] ar, int n)
{
   
    // creating the array to store
    // rearranged value
    int[] br = new int[n];
 
    // initialising pos to last index
    int pos = n - 1;
 
    // if n is odd then we will
    // transverse the array
    // from second last element
    if (n % 2 != 0)
        pos = pos - 1;
 
    // storing index of middle element
    int mid = n / 2;
 
    // index variable for rearranged array
    int c = 0;
 
    // transversing the array from
    // the pos to mid index
    // and storing it in br[] array
    for (; pos >= mid; pos--)
        br = ar[pos];
 
    // storing the first element as
    // mid value
    br = ar[0];
 
    // if n is odd then store
    // the last value in br[] the
    // transverse till 1st index
    if (n % 2 != 0)
        br = ar[n - 1];
 
    // storing the first element of
    // array as mid value
    for (; pos >= 1; pos--)
        br = ar[pos];
 
    // returning br[] array
    return br;
}
   
// Driver Code
public static void main(String[] args)
{
    int ar[] = { 2, 8, 5, 9, 10 };
    int n = ar.length;
 
    // Function Call
    int[] res = rearrange(ar, n);
 
    // Print answer
    for (int i = 0; i < n; i++)
        System.out.print(res[i]+ " ");
}
}
 
// This code is contributed by Amit Katiyar

Python3

# Python 3 Program of the above approach
 
# Function to to shift the middle
# element to the start and end of
# the array alternatively, till
# the middle element becomes equal to
# the first element of the original Array
def rearrange(ar, n):
    # creating the array to store
    # rearranged value
    br = [0 for i in range(n)]
 
    # initialising pos to last index
    pos = n - 1
 
    # if n is odd then we will
    # transverse the array
    # from second last element
    if (n % 2 != 0):
        pos = pos - 1
 
    # storing index of middle element
    mid = n // 2
 
    # index variable for rearranged array
    c = 0
 
    # transversing the array from
    # the pos to mid index
    # and storing it in br[] array
    while(pos >= mid):
        br = ar[pos]
        c += 1
        pos -= 1
 
    # storing the first element as
    # mid value
    br = ar[0]
    c += 1
 
    # if n is odd then store
    # the last value in br[] the
    # transverse till 1st index
    if (n % 2 != 0):
        br = ar[n - 1]
        c += 1
 
    # storing the first element of
    # array as mid value
    while(pos >= 1):
        br = ar[pos]
        c += 1
        pos -= 1
 
    # returning br[] array
    return br
 
# Driver Code
if __name__ == '__main__':
    ar = [2, 8, 5, 9, 10]
    n = len(ar)
 
    # Function Call
    res = rearrange(ar, n)
 
    # Print answer
    for i in range(n):
        print(res[i],end = " ")
         
        # This code is contributed by ipg2016107.

C#

// C# Program of the above approach
using System;
 
public class GFG
{
 
// Function to to shift the middle
// element to the start and end of
// the array alternatively, till
// the middle element becomes equal to
// the first element of the original Array
static int[] rearrange(int[] ar, int n)
{
   
    // creating the array to store
    // rearranged value
    int[] br = new int[n];
 
    // initialising pos to last index
    int pos = n - 1;
 
    // if n is odd then we will
    // transverse the array
    // from second last element
    if (n % 2 != 0)
        pos = pos - 1;
 
    // storing index of middle element
    int mid = n / 2;
 
    // index variable for rearranged array
    int c = 0;
 
    // transversing the array from
    // the pos to mid index
    // and storing it in br[] array
    for (; pos >= mid; pos--)
        br = ar[pos];
 
    // storing the first element as
    // mid value
    br = ar[0];
 
    // if n is odd then store
    // the last value in br[] the
    // transverse till 1st index
    if (n % 2 != 0)
        br = ar[n - 1];
 
    // storing the first element of
    // array as mid value
    for (; pos >= 1; pos--)
        br = ar[pos];
 
    // returning br[] array
    return br;
}
   
// Driver Code
public static void Main(String[] args)
{
    int []ar = { 2, 8, 5, 9, 10 };
    int n = ar.Length;
 
    // Function Call
    int[] res = rearrange(ar, n);
 
    // Print answer
    for (int i = 0; i < n; i++)
        Console.Write(res[i]+ " ");
}
}
 
// This code is contributed by Amit Katiyar

Javascript


输出
9 5 2 10 8 

时间复杂度: O(n)
空间复杂度: O(n)