VOOZH about

URL: https://www.geeksforgeeks.org/dsa/sort-even-placed-elements-increasing-odd-placed-decreasing-order/

⇱ Sort even-placed in increasing and odd-placed in decreasing order - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 28 Feb, 2025

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}
Explanation:
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}
Explanation:
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

[Naive Approach] - O(n Log n) Time and O(n) Space

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.


Output
1 2 3 6 8 9 7 5 4 0 

[Expected Approach - 1] - O(n Log n) Time and O(1) Space

The problem can also be solved without the use of Auxiliary space. The idea is to swap the first half odd index positions with the second half even index positions and then sort the first half array in increasing order and the second half array in decreasing order.


Output
1 2 3 6 8 9 7 5 4 0 

Note : The above Python and JS codes seem to require extra space. Let us know in comments about your thoughts and any alternate implementations.

[Expected Approach - 2] - O(n Log n) Time and O(1) Space

Another efficient approach to solve the problem in O(1) auxiliary space is by Using negative multiplication.

The steps involved are as follows:

  1.  Multiply all the elements at even placed index by -1.
  2. Sort the whole array. In this way, we can get all even placed index in the starting as they are negative numbers now.
  3. Now revert the sign of these elements.
  4. After this reverse the first half of the array which contains an even placed number to make it in increasing order.
  5. And then reverse the rest half of the array to make odd placed numbers in decreasing order.

Note: This method is only applicable if all the elements in the array are non-negative.

An illustrative example of the above approach:

Let given array: arr[] = {0, 1, 2, 3, 4, 5, 6, 7}
Array after multiplying by -1 to even placed elements: arr[] = {0, 1, -2, 3, -4, 5, -6, 7}
Array after sorting: arr[] = {-6, -4, -2, 0, 1, 3, 5, 7}
Array after reverting negative values: arr[] = {6, 4, 2, 0, 1, 3, 5, 7}
After reversing the first half of array: arr[] = {0, 2, 4, 6, 1, 3, 5, 7}
After reversing the second half of array: arr[] = {0, 2, 4, 6, 7, 5, 3, 1}

Below is the code for the above approach:


Output
1 2 3 6 8 9 7 5 4 0 
Comment
Article Tags:
Article Tags: