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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void bitonicGenerator( int arr[], int n)
{
vector< int > evenArr;
vector< int > oddArr;
for ( int i = 0; i < n; i++) {
if (!(i % 2))
evenArr.push_back(arr[i]);
else
oddArr.push_back(arr[i]);
}
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];
}
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
import java.util.*;
class GFG {
static void bitonicGenerator( int arr[], int n)
{
Vector<Integer> evenArr = new Vector<Integer>();
Vector<Integer> oddArr = new Vector<Integer>();
for ( int i = 0 ; i < n; i++) {
if (i % 2 != 1 ) {
evenArr.add(arr[i]);
}
else {
oddArr.add(arr[i]);
}
}
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);
}
}
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] + " " );
}
}
}
|
Python3
def bitonicGenerator(arr, n):
evenArr = []
oddArr = []
for i in range (n):
if ((i % 2 ) = = 0 ):
evenArr.append(arr[i])
else :
oddArr.append(arr[i])
evenArr = sorted (evenArr)
oddArr = sorted (oddArr)
oddArr = oddArr[:: - 1 ]
i = 0
for j in range ( len (evenArr)):
arr[i] = evenArr[j]
i + = 1
for j in range ( len (oddArr)):
arr[i] = oddArr[j]
i + = 1
arr = [ 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 ]
n = len (arr)
bitonicGenerator(arr, n)
for i in arr:
print (i, end = " " )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void bitonicGenerator( int []arr, int n)
{
List< int > evenArr = new List< int >();
List< int > oddArr = new List< int >();
int i = 0;
for (i = 0; i < n; i++)
{
if (i % 2 != 1)
{
evenArr.Add(arr[i]);
}
else
{
oddArr.Add(arr[i]);
}
}
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];
}
}
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] + " " );
}
}
}
|
Javascript
<script>
function bitonicGenerator(arr,n)
{
let evenArr = [];
let oddArr = [];
for (let i = 0; i < n; i++) {
if (i % 2 != 1) {
evenArr.push(arr[i]);
}
else {
oddArr.push(arr[i]);
}
}
evenArr.sort( function (a,b){ return a-b;});
oddArr.sort( function (a,b){ return b-a;});
let i = 0;
for (let j = 0; j < evenArr.length; j++) {
arr[i++] = evenArr[j];
}
for (let j = 0; j < oddArr.length; j++) {
arr[i++] = oddArr[j];
}
}
let arr=[1, 5, 8, 9, 6, 7, 3, 4, 2, 0 ];
let n = arr.length;
bitonicGenerator(arr, n);
for (let i = 0; i < n; i++) {
document.write(arr[i] + " " );
}
</script>
|
Output
1 2 3 6 8 9 7 5 4 0
Time Complexity: O(n Log n)
Auxiliary Space: O(n)
The above 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. Thanks to SWARUPANANDA DHUA for suggesting this.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void bitonicGenerator( int arr[], int n)
{
int i = 1;
int j = n - 1;
if (j % 2 != 0)
j--;
while (i < j) {
swap(arr[i], arr[j]);
i += 2;
j -= 2;
}
sort(arr, arr + (n + 1) / 2);
sort(arr + (n + 1) / 2, arr + n, greater< int >());
}
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
import java.util.Arrays;
class GFG {
static void bitonicGenerator( int arr[], int n)
{
int i = 1 ;
int j = n - 1 ;
if (j % 2 != 0 )
j--;
while (i < j) {
arr = swap(arr, i, j);
i += 2 ;
j -= 2 ;
}
Arrays.sort(arr, 0 , (n + 1 ) / 2 );
Arrays.sort(arr, (n + 1 ) / 2 , n);
int low = (n + 1 ) / 2 , high = n - 1 ;
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;
}
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] + " " );
}
}
|
Python3
def bitonicGenerator(arr, n):
i = 1
j = n - 1
if (j % 2 ! = 0 ):
j = j - 1
while (i < j) :
arr[j], arr[i] = arr[i], arr[j]
i = i + 2
j = j - 2
arr_f = []
arr_s = []
for i in range ( int ((n + 1 ) / 2 )) :
arr_f.append(arr[i])
i = int ((n + 1 ) / 2 )
while ( i < n ) :
arr_s.append(arr[i])
i = i + 1
arr_f.sort()
arr_s.sort(reverse = True )
for i in arr_s:
arr_f.append(i)
return arr_f
arr = [ 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 ]
n = len (arr)
arr = bitonicGenerator(arr, n)
print (arr)
|
C#
using System;
class GFG
{
static void bitonicGenerator( int []arr, int n)
{
int i = 1;
int j = n - 1;
if (j % 2 != 0)
j--;
while (i < j)
{
arr = swap(arr, i, j);
i += 2;
j -= 2;
}
Array.Sort(arr, 0, (n + 1) / 2);
Array.Sort(arr, (n + 1) / 2,
n - ((n + 1) / 2));
int low = (n + 1) / 2, high = n - 1;
while (low < high)
{
int 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;
}
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] + " " );
}
}
|
Javascript
<script>
function bitonicGenerator(arr, n)
{
let i = 1;
let j = n - 1;
if (j % 2 != 0)
j--;
while (i < j) {
arr = swap(arr, i, j);
i += 2;
j -= 2;
}
let temp1 = arr.slice(0,Math.floor((n+1)/2)).sort( function (a,b){ return a-b;});
let temp2 = arr.slice(Math.floor((n+1)/2),n).sort( function (a,b){ return b-a;});
arr = temp1.concat(temp2);
return arr;
}
function swap(arr, i, j)
{
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
let arr = [1, 5, 8, 9, 6, 7, 3, 4, 2, 0 ];
let n = arr.length;
arr = bitonicGenerator(arr, n);
document.write(arr.join( " " ));
</script>
|
Output
1 2 3 6 8 9 7 5 4 0
Time Complexity: O(n Log n)
Auxiliary Space: O(1)
Another approach:
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( int arr[], int n)
{
for ( int i = 0; i < n; i++) {
if (i % 2==0)
arr[i]=-1*arr[i];
}
sort(arr,arr+n);
int mid=(n-1)/2;
for ( int i = 0; i <= mid; i++) {
arr[i]=-1*arr[i];
}
reverse(arr,arr+mid+1);
reverse(arr+mid+1,arr+n);
}
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
import java.util.Arrays;
public class GFG {
static void reverse( int a[], int l, int r)
{
while (l<=r)
{
int temp = a[l];
a[l] = a[r];
a[r] = temp;
l++;
r--;
}
}
static void bitonicGenerator( int [] arr, int n)
{
for ( int i = 0 ; i < n; i++) {
if (i % 2 == 0 )
arr[i]=- 1 *arr[i];
}
Arrays.sort(arr);
int mid=(n- 1 )/ 2 ;
for ( int i = 0 ; i <= mid; i++) {
arr[i]=- 1 *arr[i];
}
reverse(arr, 0 ,mid);
reverse(arr,mid+ 1 ,n- 1 );
}
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]+ " " );
}
}
|
Python3
class GFG :
@staticmethod
def reverse( a, l, r) :
while (l < = r) :
temp = a[l]
a[l] = a[r]
a[r] = temp
l + = 1
r - = 1
@staticmethod
def bitonicGenerator( arr, n) :
i = 0
while (i < n) :
if (i % 2 = = 0 ) :
arr[i] = - 1 * arr[i]
i + = 1
arr.sort()
mid = int ((n - 1 ) / 2 )
i = 0
while (i < = mid) :
arr[i] = - 1 * arr[i]
i + = 1
GFG.reverse(arr, 0 , mid)
GFG.reverse(arr, mid + 1 , n - 1 )
@staticmethod
def main( args) :
arr = [ 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 ]
n = len (arr)
GFG.bitonicGenerator(arr, n)
i = 0
while (i < n) :
print ( str (arr[i]) + " " , end = "")
i + = 1
if __name__ = = "__main__" :
GFG.main([])
|
C#
using System;
using System.Linq;
using System.Collections;
public class GFG
{
public static void reverse( int [] a, int l, int r)
{
while (l <= r)
{
var temp = a[l];
a[l] = a[r];
a[r] = temp;
l++;
r--;
}
}
public static void bitonicGenerator( int [] arr, int n)
{
for ( int i = 0; i < n; i++)
{
if (i % 2 == 0)
{
arr[i] = -1 * arr[i];
}
}
Array.Sort(arr);
var mid = ( int )((n - 1) / 2);
for ( int i = 0; i <= mid; i++)
{
arr[i] = -1 * arr[i];
}
GFG.reverse(arr, 0, mid);
GFG.reverse(arr, mid + 1, n - 1);
}
public static void Main(String[] args)
{
int [] arr = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};
var n = arr.Length;
GFG.bitonicGenerator(arr, n);
for ( int i = 0; i < n; i++)
{
Console.Write(arr[i].ToString() + " " );
}
}
}
|
Javascript
function reverse(a, l, r)
{
while (l <= r)
{
var temp = a[l];
a[l] = a[r];
a[r] = temp;
l++;
r--;
}
}
function bitonicGenerator(arr, n)
{
var i=0;
for (i; i < n; i++)
{
if (i % 2 == 0)
{
arr[i] = -1 * arr[i];
}
}
arr.sort( function (a, b) { return a - b;});
var mid = parseInt((n - 1) / 2);
var i=0;
for (i; i <= mid; i++)
{
arr[i] = -1 * arr[i];
}
reverse(arr, 0, mid);
reverse(arr, mid + 1, n - 1);
}
var arr = [1, 5, 8, 9, 6, 7, 3, 4, 2, 0];
var n = arr.length;
bitonicGenerator(arr, n);
var i = 0;
for (i; i < n; i++)
{
console.log(arr[i] + " " );
}
|
Output
1 2 3 6 8 9 7 5 4 0
Time Complexity: O(n*log(n))
Auxiliary Space: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
11 Sep, 2023
Like Article
Save Article