Tutorialspoint.dev

Sort even-placed elements in increasing and odd-placed in decreasing order

We are given an array of n distinct numbers. The task is to sort all even-placed numbers in increasing and odd-placed numbers in decreasing order. The modified array should contain all sorted even-placed numbers followed by reverse sorted odd-placed numbers.

Note that the first element is considered as even placed because of its index 0.

Examples:

Input:  arr[] = {0, 1, 2, 3, 4, 5, 6, 7}
Output: arr[] = {0, 2, 4, 6, 7, 5, 3, 1}
Even-place elements : 0, 2, 4, 6
Odd-place elements : 1, 3, 5, 7
Even-place elements in increasing order : 
0, 2, 4, 6
Odd-Place elements in decreasing order : 
7, 5, 3, 1

Input: arr[] = {3, 1, 2, 4, 5, 9, 13, 14, 12}
Output: {2, 3, 5, 12, 13, 14, 9, 4, 1}
Even-place elements : 3, 2, 5, 13, 12
Odd-place elements : 1, 4, 9, 14
Even-place elements in increasing order : 
2, 3, 5, 12, 13
Odd-Place elements in decreasing order : 
14, 9, 4, 1


The idea is simple. We create two auxiliary arrays evenArr[] and oddArr[] respectively. We traverse input array and put all even-placed elements in evenArr[] and odd placed elements in oddArr[]. Then we sort evenArr[] in ascending and oddArr[] in descending order. Finally copy evenArr[] and oddArr[] to get the required result.

C++

// Program to separately sort even-placed and odd
// placed numbers and place them together in sorted
// array.
#include <bits/stdc++.h>
using namespace std;
  
void bitonicGenerator(int arr[], int n)
{
    // create evenArr[] and oddArr[]
    vector<int> evenArr;
    vector<int> oddArr;
  
    // Put elements in oddArr[] and evenArr[] as
    // per their position
    for (int i = 0; i < n; i++) {
        if (!(i % 2))
            evenArr.push_back(arr[i]);
        else
            oddArr.push_back(arr[i]);
    }
  
    // sort evenArr[] in ascending order
    // sort oddArr[] in descending order
    sort(evenArr.begin(), evenArr.end());
    sort(oddArr.begin(), oddArr.end(), greater<int>());
  
    int i = 0;
    for (int j = 0; j < evenArr.size(); j++)
        arr[i++] = evenArr[j];
    for (int j = 0; j < oddArr.size(); j++)
        arr[i++] = oddArr[j];
}
  
// Driver Program
int main()
{
    int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
    bitonicGenerator(arr, n);
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    return 0;
}

Java

// Java Program to separately sort
// even-placed and odd placed numbers
// and place them together in sorted
// array.
import java.util.*;
  
class GFG {
  
    static void bitonicGenerator(int arr[], int n)
    {
        // create evenArr[] and oddArr[]
        Vector<Integer> evenArr = new Vector<Integer>();
        Vector<Integer> oddArr = new Vector<Integer>();
  
        // Put elements in oddArr[] and evenArr[] as
        // per their position
        for (int i = 0; i < n; i++) {
            if (i % 2 != 1) {
                evenArr.add(arr[i]);
            }
            else {
                oddArr.add(arr[i]);
            }
        }
  
        // sort evenArr[] in ascending order
        // sort oddArr[] in descending order
        Collections.sort(evenArr);
        Collections.sort(oddArr, Collections.reverseOrder());
  
        int i = 0;
        for (int j = 0; j < evenArr.size(); j++) {
            arr[i++] = evenArr.get(j);
        }
        for (int j = 0; j < oddArr.size(); j++) {
            arr[i++] = oddArr.get(j);
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
        int n = arr.length;
        bitonicGenerator(arr, n);
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
  
/* This code contributed by PrinciRaj1992 */

/div>

C#

// C# Program to separately sort
// even-placed and odd placed numbers
// and place them together in sorted
// array.
using System;
using System.Collections.Generic;
  
class GFG 
{
  
    static void bitonicGenerator(int []arr, int n)
    {
        // create evenArr[] and oddArr[]
        List<int> evenArr = new List<int>();
        List<int> oddArr = new List<int>();
        int i = 0;
          
        // Put elements in oddArr[] and evenArr[] as
        // per their position
        for (i = 0; i < n; i++) 
        {
            if (i % 2 != 1) 
            {
                evenArr.Add(arr[i]);
            }
            else 
            {
                oddArr.Add(arr[i]);
            }
        }
  
        // sort evenArr[] in ascending order
        // sort oddArr[] in descending order
        evenArr.Sort();
        oddArr.Sort();
        oddArr.Reverse();
  
        i = 0;
        for (int j = 0; j < evenArr.Count; j++) 
        {
            arr[i++] = evenArr[j];
        }
        for (int j = 0; j < oddArr.Count; j++)
        {
            arr[i++] = oddArr[j];
        }
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int []arr = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
        int n = arr.Length;
        bitonicGenerator(arr, n);
        for (int i = 0; i < n; i++) 
        {
            Console.Write(arr[i] + " ");
        }
    }
}
  
// This code contributed by Rajput-Ji

Output:



1 2 3 6 8 9 7 5 4 0

Time Complexity : O(n Log n)
Space Complexity : O(n)

The above problem can also be solved without use of Auxiliary space. The idea is to swaps first half odd index positions with second half even index positions and then sort the first half array in increasing order and second half array in decreasing order. Thanks to SWARUPANANDA DHUA for suggesting this.

C++

// Program to sort even-placed elements in increasing and
// odd-placed in decreasing order with constant space complexity
  
#include <bits/stdc++.h>
using namespace std;
  
void bitonicGenerator(int arr[], int n)
{
    // first odd index
    int i = 1;
  
    // last index
    int j = n - 1;
  
    // if last index is odd
    if (j % 2 != 0)
        // decrement j to even index
        j--;
  
    // swapping till half of array
    while (i < j) {
        swap(arr[i], arr[j]);
        i += 2;
        j -= 2;
    }
  
    // Sort first half in increasing
    sort(arr, arr + (n + 1) / 2);
  
    // Sort second half in decreasing
    sort(arr + (n + 1) / 2, arr + n, greater<int>());
}
  
// Driver Program
int main()
{
    int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
    bitonicGenerator(arr, n);
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    return 0;
}
// This code is contributed by SWARUPANANDA DHUA

Java

// Program to sort even-placed elements in increasing and
// odd-placed in decreasing order with constant space complexity
import java.util.Arrays;
class GFG {
  
    static void bitonicGenerator(int arr[], int n)
    {
        // first odd index
        int i = 1;
  
        // last index
        int j = n - 1;
  
        // if last index is odd
        if (j % 2 != 0)
            // decrement j to even index
            j--;
  
        // swapping till half of array
        while (i < j) {
            arr = swap(arr, i, j);
            i += 2;
            j -= 2;
        }
  
        // Sort first half in increasing
        Arrays.sort(arr, 0, (n + 1) / 2);
  
        // Sort second half in decreasing
        Arrays.sort(arr, (n + 1) / 2, n);
        int low = (n + 1) / 2, high = n - 1;
        // Reverse the second half
        while (low < high) {
            Integer temp = arr[low];
            arr[low] = arr[high];
            arr[high] = temp;
            low++;
            high--;
        }
    }
    static int[] swap(int[] arr, int i, int j)
    {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
        return arr;
    }
    // Driver Program
    public static void main(String[] args)
    {
        int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
        int n = arr.length;
        bitonicGenerator(arr, n);
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
}
// This code has been contributed by 29AjayKumar

Output:

1 2 3 6 8 9 7 5 4 0

Time Complexity : O(n Log n)
Space Complexity : O(1)

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter