📌  相关文章
📜  重新排列一个数组,使每个奇数索引元素都大于它之前的元素

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

重新排列一个数组,使每个奇数索引元素都大于它之前的元素

给定一个未排序的数组,重新排列数组,使奇数索引处的数字大于前一个偶数索引处的数字。可能有多个输出,我们需要打印其中一个。

注意:索引是基于数组的,所以它总是从 0 开始。

例子:

Input  : arr[] = {5, 2, 3, 4}
Output : arr[] = {2, 5, 3, 4}

Input  : arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}
Output : arr[] = {1, 3, 2, 5, 4, 7, 6, 9, 8} 

如果给定数组的长度是偶数,那么它可以在一次运行中完成。仅对于每对邻居,将最大值放在奇数位置,将最小值放在偶数位置。所以我们只是从头开始一个循环并比较每两个元素。比方说
6, 5, 4, 3, 2, 1 会给我们 5, 6, 3, 4, 1, 2
如果数组的长度是奇数,比如 6, 5, 4, 3, 2, 1, 100 那就有点棘手了,我们需要再次运行第二个反向循环,但从最后一个元素开始。因此,第一个循环后的 6、5、4、3、2、1、100 将是 5、6、3、4、1、2、100,第二个循环后为 5、6、3、4、1、100、2

C++
// C++ program to rearrange array such that
// odd indexed elements are greater.
#include
using namespace std;
 
void rearrange(int arr[], int n)
{
    // Common code for odd and even lengths
    for (int i=0; i arr[i+1])
            swap(arr[i], arr[i+1]);
    }
 
    // If length is odd
    if (n & 1)
    {
        for (int i=n-1; i>0; i=i-2)
            if (arr[i] > arr[i-1])
                swap(arr[i], arr[i-1]);
    }
}
 
/* Utility that prints out an array on a line */
void printArray(int arr[], int size)
{
    for (int i=0; i < size; i++)
        printf("%d ", arr[i]);
 
    printf("\n");
}
 
/* Driver function to test above functions */
int main()
{
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << "Before rearranging\n";
    printArray(arr, n);
    rearrange(arr, n);
    cout << "After rearranging\n";
    printArray(arr, n);
    return 0;
}


Java
// Java program to rearrange array such that
// odd indexed elements are greater.
 
import java.util.Arrays;
 
class GFG
{
    // Utility function to Swap two variables
    public static void swap(int[] arr, int i, int j)
    {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
 
    // method to rearrange array such that
    // odd indexed elements are greater.
    public static void Rearrange(int[] arr, int n)
    {
        // Common code for odd and even lengths
        for (int i = 0; i < n - 1; i = i + 2) {
            if (arr[i] > arr[i + 1])
                swap(arr, i, i + 1);
        }
 
        // If length is odd
        if ((n & 1) > 0) {
            for (int i = n - 1; i > 0; i = i - 2)
                if (arr[i] > arr[i - 1])
                    swap(arr, i, i - 1);
        }
    }
 
    // Driver Method
    public static void main(String[] args)
    {
        int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 
        System.out.println("Before rearranging");
        System.out.println(Arrays.toString(arr));
 
        Rearrange(arr, arr.length);       
 
        System.out.println("After rearranging");
        System.out.println(Arrays.toString(arr));
    }
}
/* This code is contributed by Mr. Somesh Awasthi */


Python3
# Python 3 program to rearrange array such
# that odd indexed elements are greater.
def rearrange(arr, n):
     
    # Common code for odd and even lengths
    for i in range(0, n - 1, 2):
        if (arr[i] > arr[i + 1]):
            temp = arr[i]
            arr[i] = arr[i + 1]
            arr[i + 1] = temp
 
    # If length is odd
    if (n & 1):
        i = n - 1
        while(i > 0):
            if (arr[i] > arr[i - 1]):
                temp = arr[i]
                arr[i] = arr[i - 1]
                arr[i - 1] = temp
            i -= 2
 
# Utility that prints out an
# array on a line
def printArray(arr, size):
    for i in range(0, size, 1):
        print(arr[i], end = " ")
    print("\n")
     
# Driver Code
if __name__ == '__main__':
    arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    n = len(arr)
    print("Before rearranging")
     
    printArray(arr, n)
    rearrange(arr, n)
    print("After rearranging")
     
    printArray(arr, n)
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to rearrange
// array such that odd
// indexed elements are
// greater.
using System;
 
class GFG
{
 
// Utility function to
// Swap two variables
public static void swap(int[] arr,
                        int i, int j)
{
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}
 
// method to rearrange
// array such that odd
// indexed elements are
// greater.
public static void Rearrange(int[] arr,
                            int n)
{
    // Common code for odd
    // and even lengths
    for (int i = 0; i < n - 1; i = i + 2)
    {
        if (arr[i] > arr[i + 1])
            swap(arr, i, i + 1);
    }
 
    // If length is odd
    if ((n & 1) > 0)
    {
        for (int i = n - 1;
                 i > 0; i = i - 2)
            if (arr[i] > arr[i - 1])
                swap(arr, i, i - 1);
    }
}
 
// Driver Code
static void Main()
{
    int[] arr = {1, 2, 3, 4,
                 5, 6, 7, 8, 9};
 
    Console.WriteLine("Before rearranging");
    for(int i = 0; i < arr.Length; i++)
        Console.Write(arr[i] + " ");
 
    Rearrange(arr, arr.Length);    
 
    Console.WriteLine("\nAfter rearranging");
    for(int i = 0; i < arr.Length; i++)
        Console.Write(arr[i] + " ");
}
}
 
// This code is contributed
// by Sam007


PHP
 $arr[$i + 1])
        {
            $temp = $arr[$i];
            $arr[$i] = $arr[$i + 1];
            $arr[$i + 1] = $temp;
        }
    }
 
    // If length is odd
    if ($n & 1)
    {
        for ($i = $n - 1; $i > 0; $i = $i - 2)
            if ($arr[$i] > $arr[$i - 1])
            {
                $temp = $arr[$i];
                $arr[$i] = $arr[$i - 1];
                $arr[$i - 1] = $temp;
            }
    }
}
 
// Utility that prints out an array on a line
function printArray(&$arr, $size)
{
    for ($i = 0; $i < $size; $i++)
        echo $arr[$i] . " ";
 
    echo "\n";
}
 
// Driver Code
$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
$n = sizeof($arr);
echo "Before rearranging\n";
printArray($arr, $n);
rearrange($arr, $n);
 
echo "After rearranging\n";
printArray($arr, $n);
 
// This code is contributed by ita_c
?>


Javascript


输出:

Before rearranging
1 2 3 4 5 6 7 8 9 
After rearranging
1 3 2 5 4 7 6 9 8 

时间复杂度: O(n)

辅助空间: O(1)