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.
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(vector<int>& arr)
{
// 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 < arr.size(); 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()
{
vector<int> arr = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
bitonicGenerator(arr);
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
Java
// Program to separately sort even-placed and odd
// placed numbers and place them together in sorted
// array.
import java.util.*;
public class Main {
public static void bitonicGenerator(int[] arr) {
// create evenArr[] and oddArr[]
List<Integer> evenArr = new ArrayList<>();
List<Integer> oddArr = new ArrayList<>();
// Put elements in oddArr[] and evenArr[] as
// per their position
for (int i = 0; i < arr.length; i++) {
if (i % 2 == 0)
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 num : evenArr)
arr[i++] = num;
for (int num : oddArr)
arr[i++] = num;
}
public static void main(String[] args) {
int[] arr = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
bitonicGenerator(arr);
for (int num : arr)
System.out.print(num + " ");
}
}
Python
# Program to separately sort even-placed and odd
# placed numbers and place them together in sorted
# array.
def bitonic_generator(arr):
# create evenArr[] and oddArr[]
evenArr = []
oddArr = []
# Put elements in oddArr[] and evenArr[] as
# per their position
for i in range(len(arr)):
if i % 2 == 0:
evenArr.append(arr[i])
else:
oddArr.append(arr[i])
# sort evenArr[] in ascending order
# sort oddArr[] in descending order
evenArr.sort()
oddArr.sort(reverse=True)
i = 0
for num in evenArr:
arr[i] = num
i += 1
for num in oddArr:
arr[i] = num
i += 1
# Driver Program
arr = [1, 5, 8, 9, 6, 7, 3, 4, 2, 0]
bitonic_generator(arr)
print(' '.join(map(str, arr)))
C#
// Program to separately sort even-placed and odd
// placed numbers and place them together in sorted
// array.
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void BitonicGenerator(int[] arr) {
// create evenArr[] and oddArr[]
List<int> evenArr = new List<int>();
List<int> oddArr = new List<int>();
// Put elements in oddArr[] and evenArr[] as
// per their position
for (int i = 0; i < arr.Length; i++) {
if (i % 2 == 0)
evenArr.Add(arr[i]);
else
oddArr.Add(arr[i]);
}
// sort evenArr[] in ascending order
// sort oddArr[] in descending order
evenArr.Sort();
oddArr.Sort((a, b) => b.CompareTo(a));
int index = 0;
foreach (var num in evenArr)
arr[index++] = num;
foreach (var num in oddArr)
arr[index++] = num;
}
static void Main() {
int[] arr = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
BitonicGenerator(arr);
Console.WriteLine(string.Join(" ", arr));
}
}
JavaScript
// Program to separately sort even-placed and odd
// placed numbers and place them together in sorted
// array.
function bitonicGenerator(arr) {
// create evenArr[] and oddArr[]
const evenArr = [];
const oddArr = [];
// Put elements in oddArr[] and evenArr[] as
// per their position
for (let i = 0; i < arr.length; i++) {
if (i % 2 === 0)
evenArr.push(arr[i]);
else
oddArr.push(arr[i]);
}
// sort evenArr[] in ascending order
// sort oddArr[] in descending order
evenArr.sort((a, b) => a - b);
oddArr.sort((a, b) => b - a);
let i = 0;
for (const num of evenArr)
arr[i++] = num;
for (const num of oddArr)
arr[i++] = num;
}
// Driver Program
const arr = [1, 5, 8, 9, 6, 7, 3, 4, 2, 0];
bitonicGenerator(arr);
console.log(arr.join(' '));
PHP
// Program to separately sort even-placed and odd
// placed numbers and place them together in sorted
// array.
function bitonicGenerator(&$arr) {
// create evenArr[] and oddArr[]
$evenArr = [];
$oddArr = [];
// Put elements in oddArr[] and evenArr[] as
// per their position
foreach ($arr as $i => $value) {
if ($i % 2 === 0)
$evenArr[] = $value;
else
$oddArr[] = $value;
}
// sort evenArr[] in ascending order
// sort oddArr[] in descending order
sort($evenArr);
rsort($oddArr);
$i = 0;
foreach ($evenArr as $num) {
$arr[$i++] = $num;
}
foreach ($oddArr as $num) {
$arr[$i++] = $num;
}
}
// Driver Program
$arr = [1, 5, 8, 9, 6, 7, 3, 4, 2, 0];
bitonicGenerator($arr);
echo implode(' ', $arr);
Output1 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.
C++
#include <bits/stdc++.h>
using namespace std;
void bitonicGenerator(vector<int>& arr)
{
// first odd index
int i = 1;
// last index
int n = arr.size();
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.begin(), arr.begin() + (n + 1) / 2);
// Sort second half in decreasing
sort(arr.begin() + (n + 1) / 2, arr.end(), greater<int>());
}
// Driver Program
int main()
{
vector<int> arr = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
bitonicGenerator(arr);
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
Java
import java.util.Arrays;
class BitonicGenerator {
public static void bitonicGenerator(int[] arr) {
// first odd index
int i = 1;
// last index
int n = arr.length;
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) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i += 2;
j -= 2;
}
// Sort first half in increasing order
Arrays.sort(arr, 0, (n + 1) / 2);
// Sort second half in decreasing order
Arrays.sort(arr, (n + 1) / 2, n);
reverse(arr, (n + 1) / 2, n);
}
private static void reverse(int[] arr, int start, int end) {
end--;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
// Driver Program
public static void main(String[] args) {
int[] arr = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
bitonicGenerator(arr);
for (int num : arr) {
System.out.print(num + " ");
}
}
}
Python
def bitonic_generator(arr):
# first odd index
i = 1
# last index
n = len(arr)
j = n - 1
# if last index is odd
if j % 2 != 0:
# decrement j to even index
j -= 1
# swapping till half of array
while i < j:
arr[i], arr[j] = arr[j], arr[i]
i += 2
j -= 2
# Sort first half in increasing
arr[:(n + 1) // 2] = sorted(arr[:(n + 1) // 2])
# Sort second half in decreasing
arr[(n + 1) // 2:] = sorted(arr[(n + 1) // 2:], reverse=True)
# Driver Program
arr = [1, 5, 8, 9, 6, 7, 3, 4, 2, 0]
bitonic_generator(arr)
print(' '.join(map(str, arr)))
C#
// Function to generate a bitonic sequence
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void BitonicGenerator(List<int> arr)
{
// first odd index
int i = 1;
// last index
int n = arr.Count;
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)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i += 2;
j -= 2;
}
// Sort first half in increasing
arr.Sort(0, (n + 1) / 2);
// Sort second half in decreasing
arr.Sort((n + 1) / 2, n - (n + 1) / 2, Comparer<int>.Create((x, y) => y.CompareTo(x)));
}
// Driver Program
static void Main()
{
List<int> arr = new List<int> { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
BitonicGenerator(arr);
Console.WriteLine(string.Join(" ", arr));
}
}
JavaScript
// Function to generate a bitonic sequence
function bitonicGenerator(arr) {
// first odd index
let i = 1;
// last index
let n = arr.length;
let 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[i], arr[j]] = [arr[j], arr[i]];
i += 2;
j -= 2;
}
// Sort first half in increasing
arr.sort((a, b) => a - b);
// Sort second half in decreasing
arr.slice((n + 1) / 2).sort((a, b) => b - a);
}
// Driver Program
let arr = [1, 5, 8, 9, 6, 7, 3, 4, 2, 0];
bitonicGenerator(arr);
console.log(arr.join(' '));
Output1 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:
- Multiply all the elements at even placed index by -1.
- Sort the whole array. In this way, we can get all even placed index in the starting as they are negative numbers now.
- Now revert the sign of these elements.
- After this reverse the first half of the array which contains an even placed number to make it in increasing order.
- 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:
C++
#include <bits/stdc++.h>
using namespace std;
void bitonicGenerator(vector<int>& arr)
{
// Making all even placed index
// element negative
for (int i = 0; i < arr.size(); i++) {
if (i % 2==0)
arr[i] = -1 * arr[i];
}
// Sorting the whole array
sort(arr.begin(), arr.end());
// Finding the middle value of
// the array
int mid = (arr.size() - 1) / 2;
// Reverting the changed sign
for (int i = 0; i <= mid; i++) {
arr[i] = -1 * arr[i];
}
// Reverse first half of array
reverse(arr.begin(), arr.begin() + mid + 1);
// Reverse second half of array
reverse(arr.begin() + mid + 1, arr.end());
}
// Driver Program
int main()
{
vector<int> arr = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
bitonicGenerator(arr);
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
Java
import java.util.Arrays;
import java.util.List;
public class BitonicGenerator {
public static void bitonicGenerator(List<Integer> arr) {
// Making all even placed index
// element negative
for (int i = 0; i < arr.size(); i++) {
if (i % 2 == 0)
arr.set(i, -1 * arr.get(i));
}
// Sorting the whole array
Collections.sort(arr);
// Finding the middle value of
// the array
int mid = (arr.size() - 1) / 2;
// Reverting the changed sign
for (int i = 0; i <= mid; i++) {
arr.set(i, -1 * arr.get(i));
}
// Reverse first half of array
Collections.reverse(arr.subList(0, mid + 1));
// Reverse second half of array
Collections.reverse(arr.subList(mid + 1, arr.size()));
}
// Driver Program
public static void main(String[] args) {
List<Integer> arr = Arrays.asList(1, 5, 8, 9, 6, 7, 3, 4, 2, 0);
bitonicGenerator(arr);
for (int i : arr)
System.out.print(i + " ");
}
}
Python
def bitonic_generator(arr):
# Making all even placed index
# element negative
for i in range(len(arr)):
if i % 2 == 0:
arr[i] = -1 * arr[i]
# Sorting the whole array
arr.sort()
# Finding the middle value of
# the array
mid = (len(arr) - 1) // 2
# Reverting the changed sign
for i in range(mid + 1):
arr[i] = -1 * arr[i]
# Reverse first half of array
arr[:mid + 1] = reversed(arr[:mid + 1])
# Reverse second half of array
arr[mid + 1:] = reversed(arr[mid + 1:])
# Driver Program
arr = [1, 5, 8, 9, 6, 7, 3, 4, 2, 0]
bitonic_generator(arr)
print(' '.join(map(str, arr)))
C#
using System;
using System.Collections.Generic;
using System.Linq;
class BitonicGenerator {
public static void BitonicGeneratorMethod(List<int> arr) {
// Making all even placed index
// element negative
for (int i = 0; i < arr.Count; i++) {
if (i % 2 == 0)
arr[i] = -1 * arr[i];
}
// Sorting the whole array
arr.Sort();
// Finding the middle value of
// the array
int mid = (arr.Count - 1) / 2;
// Reverting the changed sign
for (int i = 0; i <= mid; i++) {
arr[i] = -1 * arr[i];
}
// Reverse first half of array
arr.Take(mid + 1).Reverse().ToList().CopyTo(arr);
// Reverse second half of array
arr.Skip(mid + 1).Reverse().ToList().CopyTo(arr, mid + 1);
}
// Driver Program
public static void Main() {
List<int> arr = new List<int> { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
BitonicGeneratorMethod(arr);
Console.WriteLine(string.Join(" ", arr));
}
}
JavaScript
function bitonicGenerator(arr) {
// Making all even placed index
// element negative
for (let i = 0; i < arr.length; i++) {
if (i % 2 === 0)
arr[i] = -1 * arr[i];
}
// Sorting the whole array
arr.sort((a, b) => a - b);
// Finding the middle value of
// the array
const mid = Math.floor((arr.length - 1) / 2);
// Reverting the changed sign
for (let i = 0; i <= mid; i++) {
arr[i] = -1 * arr[i];
}
// Reverse first half of array
arr.slice(0, mid + 1).reverse().forEach((val, index) => arr[index] = val);
// Reverse second half of array
arr.slice(mid + 1).reverse().forEach((val, index) => arr[mid + 1 + index] = val);
}
// Driver Program
let arr = [1, 5, 8, 9, 6, 7, 3, 4, 2, 0];
bitonicGenerator(arr);
console.log(arr.join(' '));
Output1 2 3 6 8 9 7 5 4 0
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem