给我们一个数组。我们需要按升序对偶数定位的元素和按降序排序的奇数定位的元素进行排序。我们必须应用插入排序对它们进行排序。
例子:
Input : a[] = {7, 10, 11, 3, 6, 9, 2, 13, 0}
Output : 11 3 7 9 6 10 2 13 0
Even positioned elements after sorting int
ascending order : 3 9 10 13
Odd positioned elements after sorting int
descending order : 11 7 6 2 0
我们将插入排序技术分别应用于偶数定位元素和奇数定位元素,但在同一数组中。循环从第0个索引(第一个元素)开始的奇数位置开始,从第一个索引(第2个元素)开始的偶数位置开始循环,并由于每个备用位置都是奇数/偶数位置而继续增加2。
现在,我们仅将插入排序过程应用于奇数位置和偶数位置。奇数定位的元素为A [0,2,4,…],偶数为A [1,3,5,7 ..]。因此,它们被视为单独的子数组,但在同一数组中。
奇数位置的说明:
第0个元素已排序。现在,将第2个元素与第0个元素进行比较并插入,以此类推,将第(i + 2)个元素与先前的元素进行比较,直到数组结束。相同的方法适用于元素中均匀定位的元素
数组。(这与插入排序技术相同)。
C++
// C++ program to sort even positioned elements
// in ascending order and odd positioned
// elements in descending order.
#include
#include
// Function to calculate the given problem.
void evenOddInsertionSort(int arr[], int n)
{
for (int i = 2; i < n; i++)
{
int j = i-2;
int temp = arr[i];
/* checking whether the position is even
or odd. And after checking applying the
insertion sort to the given
positioned elements.*/
// checking for odd positioned.
if ((i+1) & 1 == 1)
{
// Inserting even positioned elements
// in ascending order.
while (temp >= arr[j] && j >= 0)
{
arr[j+2] = arr[j];
j -= 2;
}
arr[j+2] = temp;
}
// sorting the even positioned.
else {
// Inserting odd positioned elements
// in descending order.
while (temp <= arr[j] && j >= 0)
{
arr[j+2] = arr[j];
j -= 2;
}
arr[j+2] = temp;
}
}
}
// A utility function to print an array of size n
void printArray(int arr[], int n)
{
for (int i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
/* Driver program to test insertion sort */
int main()
{
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr)/sizeof(arr[0]);
evenOddInsertionSort(arr, n);
printArray(arr, n);
return 0;
}
Java
// Java program to sort even positioned elements
// in ascending order and odd positioned
// elements in descending order.
class GFG
{
// Function to calculate the given problem.
static void evenOddInsertionSort(int arr[], int n)
{
for (int i = 2; i < n; i++)
{
int j = i - 2;
int temp = arr[i];
/* checking whether the position is even
or odd. And after checking applying the
insertion sort to the given
positioned elements.*/
// checking for odd positioned.
if (((i + 1) & 1) == 1)
{
// Inserting even positioned elements
// in ascending order.
while (j >= 0 && temp >= arr[j])
{
arr[j + 2] = arr[j];
j -= 2;
}
arr[j + 2] = temp;
}
// sorting the even positioned.
else
{
// Inserting odd positioned elements
// in descending order.
while (j >= 0 && temp <= arr[j])
{
arr[j + 2] = arr[j];
j -= 2;
}
arr[j + 2] = temp;
}
}
}
// A utility function to print an array of size n
static void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
System.out.printf("%d ", arr[i]);
}
System.out.printf("\n");
}
/* Driver program to test insertion sort */
public static void main(String[] args)
{
int arr[] = {12, 11, 13, 5, 6};
int n = arr.length;
evenOddInsertionSort(arr, n);
printArray(arr, n);
}
}
// This code contributed by Rajput-Ji
Python3
# Python3 program to sort even
# positioned elements in ascending
# order and odd positionedelements
# in descending order.
# Function to calculate
# the given problem.
def evenOddInsertionSort(arr, n):
for i in range(2, n):
j = i - 2
temp = arr[i]
# checking whether the position
# is even or odd. And after
# checking applying the insertion
# sort to the given
# positioned elements.
# checking for odd positioned.
if ((i + 1) & 1 == 1) :
# Inserting even positioned elements
# in ascending order.
while (temp >= arr[j] and j >= 0):
arr[j + 2] = arr[j]
j -= 2
arr[j + 2] = temp
# sorting the even positioned.
else :
# Inserting odd positioned elements
# in descending order.
while (temp <= arr[j] and j >= 0) :
arr[j + 2] = arr[j]
j -= 2
arr[j + 2] = temp
# A utility function to print an array of size n
def printArray(arr, n):
for i in range(0, n):
print(arr[i], end=" ")
# Driver program
arr = [12, 11, 13, 5, 6]
n = len(arr)
evenOddInsertionSort(arr, n)
printArray(arr, n)
# This code is contributed by
# Smitha Dinesh Semwal
C#
// C# program to sort even positioned elements
// in ascending order and odd positioned
// elements in descending order.
using System;
class GFG
{
// Function to calculate the given problem.
static void evenOddInsertionSort(int []arr, int n)
{
for (int i = 2; i < n; i++)
{
int j = i - 2;
int temp = arr[i];
/* checking whether the position is even
or odd. And after checking applying the
insertion sort to the given
positioned elements.*/
// checking for odd positioned.
if (((i + 1) & 1) == 1)
{
// Inserting even positioned elements
// in ascending order.
while (j >= 0 && temp >= arr[j])
{
arr[j + 2] = arr[j];
j -= 2;
}
arr[j + 2] = temp;
}
// sorting the even positioned.
else
{
// Inserting odd positioned elements
// in descending order.
while (j >= 0 && temp <= arr[j])
{
arr[j + 2] = arr[j];
j -= 2;
}
arr[j + 2] = temp;
}
}
}
// A utility function to print an array of size n
static void printArray(int []arr, int n)
{
for (int i = 0; i < n; i++)
{
Console.Write("{0} ", arr[i]);
}
Console.Write("\n");
}
/* Driver code */
public static void Main(String[] args)
{
int []arr = {12, 11, 13, 5, 6};
int n = arr.Length;
evenOddInsertionSort(arr, n);
printArray(arr, n);
}
}
// This code has been contributed by 29AjayKumar
Javascript
输出:
13 5 12 11 6
时间复杂度: O(n 2 )
辅助空间: O(1)
存在更好的方法来解决此问题而无需插入排序。有关详细信息,请参阅按升序排列偶数元素和按降序排列奇数元素。