We are given an array. We need to sort the even positioned elements in the ascending order and the odd positioned elements in the descending order. We must apply insertion sort to sort them.
Examples: 
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
We separately apply the insertion sort technique on the even positioned elements and the odd positioned elements separately but within the same array. The loop starts for the odd positioned from the 0th index(1st element) and the for the even from the 1st index(2nd element) and keep on increasing by 2 since every alternate is odd/even positioned. 
We now simply apply the insertion sort procedure on the odd positioned and even positioned. the odd positioned elements are A[0, 2, 4, …] and even are A[1, 3, 5, 7..]. So they are considered as separate sub-arrays but within the same array. 
Explanation for the odd positioned:
The 0th element already sorted. Now the 2nd element compared with the 0th and inserted and so on the (i+2)th element compared with the previous ones until the end of the array. The same approach is applied for the even positioned elements in 
the array.(This is same as the insertion sort technique). 
C++
| // C++ program to sort even positioned elements// in ascending order and odd positioned// elements in descending order.#include<stdio.h>#include<stdlib.h>// Function to calculate the given problem.voidevenOddInsertionSort(intarr[], intn){  for(inti = 2; i < n; i++)  {      intj = i-2;      inttemp = 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 nvoidprintArray(intarr[], intn){    for(inti=0; i < n; i++)        printf("%d ", arr[i]);    printf("\n");}/* Driver program to test insertion sort */intmain(){    intarr[] = {12, 11, 13, 5, 6};    intn = sizeof(arr)/sizeof(arr[0]);    evenOddInsertionSort(arr, n);    printArray(arr, n);    return0;} | 
Java
| // Java program to sort even positioned elements // in ascending order and odd positioned // elements in descending order.classGFG {    // Function to calculate the given problem.     staticvoidevenOddInsertionSort(intarr[], intn)     {        for(inti = 2; i < n; i++)        {            intj = i - 2;            inttemp = 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     staticvoidprintArray(intarr[], intn)    {        for(inti = 0; i < n; i++)         {            System.out.printf("%d ", arr[i]);        }        System.out.printf("\n");    }    /* Driver program to test insertion sort */    publicstaticvoidmain(String[] args)    {        intarr[] = {12, 11, 13, 5, 6};        intn = 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.defevenOddInsertionSort(arr, n):    fori inrange(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] andj >=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] andj >=0) :                            arr[j +2] =arr[j]                j -=2                        arr[j +2] =temp                     # A utility function to print an array of size ndefprintArray(arr, n):        fori inrange(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.usingSystem;classGFG {    // Function to calculate the given problem.     staticvoidevenOddInsertionSort(int[]arr, intn)     {        for(inti = 2; i < n; i++)        {            intj = i - 2;            inttemp = 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     staticvoidprintArray(int[]arr, intn)    {        for(inti = 0; i < n; i++)         {            Console.Write("{0} ", arr[i]);        }        Console.Write("\n");    }    /* Driver code */    publicstaticvoidMain(String[] args)    {        int[]arr = {12, 11, 13, 5, 6};        intn = arr.Length;        evenOddInsertionSort(arr, n);        printArray(arr, n);    }}// This code has been contributed by 29AjayKumar | 
Javascript
| <script>// JavaScript program to sort even positioned elements// in ascending order and odd positioned// elements in descending order.    // Function to calculate the given problem.    functionevenOddInsertionSort(arr, n)    {        for(let i = 2; i < n; i++)        {            let j = i - 2;            let 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     functionprintArray(arr, n)    {        for(let i = 0; i < n; i++)        {            document.write(arr[i] + " ");        }        document.write();    }   // Driver code        let arr = [12, 11, 13, 5, 6];        let n = arr.length;         evenOddInsertionSort(arr, n);        printArray(arr, n);</script> | 
Output:
13 5 12 11 6
Time Complexity: O(n2)
Auxiliary Space: O(1)
There exist better approaches to solve this problem without insertion sort. Please refer Sort even-placed elements in increasing and odd-placed in decreasing order for details.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!


 
                                    







