Sort even and odd placed elements in increasing order
Given a list N, the task is to sort all the elements in odd and even positions in increasing order. After sorting, we need to put all the odd positioned elements together, then all the even positioned elements.
Examples:
Input : [3, 2, 7, 6, 8]
Output : 3 7 8 2 6
Explanation:
Odd position elements in sorted order are 3, 7, 8.
Even position elements in sorted order are 2, 6.
Input : [1, 0, 2, 7, 0, 0]
Output : 0 1 2 0 0 7
Odd {1, 2, 0}
Even {0, 7, 0}
Approach:
- Initialize two lists to store the odd and even indexed digits.
- Traverse through all the digits and store the odd indexed digits at odd_indexes list and the even indexed digits at even_indexes list.
- Print the elements in the odd_indexes list in sorted order.
- Print the elements in the even_indexes list in sorted order.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void odd_even( int arr[], int n)
{
vector< int > odd_indexes;
vector< int >even_indexes;
for ( int i = 0; i < n;i++)
{
if (i % 2 == 0)
odd_indexes.push_back(arr[i]);
else
even_indexes.push_back(arr[i]);
}
sort(odd_indexes.begin(), odd_indexes.end());
sort(even_indexes.begin(), even_indexes.end());
for ( int i = 0; i < odd_indexes.size();i++)
cout << odd_indexes[i] << " " ;
for ( int i = 0; i < even_indexes.size(); i++)
cout << even_indexes[i] << " " ;
}
int main()
{
int arr[] = {3, 2, 7, 6, 8};
int n = sizeof (arr)/ sizeof (arr[0]);
odd_even(arr, n);
}
|
Java
import java.util.*;
class GFG
{
static void odd_even( int arr[], int n)
{
Vector<Integer> odd_indexes = new Vector<Integer>();
Vector<Integer> even_indexes = new Vector<Integer>();
for ( int i = 0 ; i < n;i++)
{
if (i % 2 == 0 )
odd_indexes.add(arr[i]);
else
even_indexes.add(arr[i]);
}
Collections.sort(odd_indexes);
Collections.sort(even_indexes);
for ( int i = 0 ; i < odd_indexes.size(); i++)
System.out.print(odd_indexes.get(i) + " " );
for ( int i = 0 ; i < even_indexes.size(); i++)
System.out.print(even_indexes.get(i) + " " );
}
public static void main(String[] args)
{
int arr[] = { 3 , 2 , 7 , 6 , 8 };
int n = arr.length;
odd_even(arr, n);
}
}
|
Python3
def odd_even(n):
odd_indexes = []
even_indexes = []
for i in range ( len (n)):
if i % 2 = = 0 : odd_indexes.append(n[i])
else : even_indexes.append(n[i])
for i in sorted (odd_indexes): print (i, end = " " )
for i in sorted (even_indexes): print (i, end = " " )
n = [ 3 , 2 , 7 , 6 , 8 ]
odd_even(n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void odd_even( int []arr, int n)
{
List< int > odd_indexes = new List< int >();
List< int > even_indexes = new List< int >();
for ( int i = 0; i < n;i++)
{
if (i % 2 == 0)
odd_indexes.Add(arr[i]);
else
even_indexes.Add(arr[i]);
}
odd_indexes.Sort();
even_indexes.Sort();
for ( int i = 0; i < odd_indexes.Count; i++)
Console.Write(odd_indexes[i] + " " );
for ( int i = 0; i < even_indexes.Count; i++)
Console.Write(even_indexes[i] + " " );
}
public static void Main(String[] args)
{
int []arr = {3, 2, 7, 6, 8};
int n = arr.Length;
odd_even(arr, n);
}
}
|
PHP
<?php
function odd_even( $n )
{
$odd_indexes = array ();
$even_indexes = array ();
for ( $i = 0; $i < sizeof( $n ); $i ++)
{
if ( $i % 2 == 0)
array_push ( $odd_indexes , $n [ $i ]);
else
array_push ( $even_indexes , $n [ $i ]);
}
sort( $odd_indexes );
for ( $i = 0; $i < sizeof( $odd_indexes ); $i ++)
echo $odd_indexes [ $i ], " " ;
sort( $even_indexes ) ;
for ( $i = 0; $i < sizeof( $even_indexes ); $i ++)
echo $even_indexes [ $i ], " " ;
}
$n = array (3, 2, 7, 6, 8);
odd_even( $n );
?>
|
Javascript
<script>
function odd_even(arr, n)
{
var odd_indexes = [];
var even_indexes = [];
for ( var i = 0; i < n;i++)
{
if (i % 2 == 0)
odd_indexes.push(arr[i]);
else
even_indexes.push(arr[i]);
}
odd_indexes.sort();
even_indexes.sort();
for ( var i = 0; i < odd_indexes.length;i++)
document.write( odd_indexes[i] + " " );
for ( var i = 0; i < even_indexes.length; i++)
document.write( even_indexes[i] + " " );
}
var arr = [3, 2, 7, 6, 8];
var n = arr.length;
odd_even(arr, n);
</script>
|
Time Complexity: O(n*log(n)) //the inbuilt sort function takes N log N time to complete all operations, hence the overall time taken by the algorithm is N log N
Auxiliary Space: O(n) // since two vectors are used so the space taken by the algorithm is worst case is linear
Another Approach: The above problem can also be solved without the use of Auxiliary space. The idea is to swap the first half even positions element with the second half odd positions element. In this way, all oddly positioned elements will come in the first half while all evenly positioned elements will come in the second half of the array. After doing this we will sort the first half and then the second half of the array in increasing order individually.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void odd_even( 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);
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
}
int main()
{
int arr[] = { 3, 2, 7, 6, 8 };
int n = sizeof (arr) / sizeof (arr[0]);
odd_even(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
static void odd_even( int [] arr, int n) {
int i = 1 ;
int j = n - 1 ;
if (j % 2 != 0 )
j--;
while (i < j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i += 2 ;
j -= 2 ;
}
Arrays.sort(arr, 0 , (n + 1 ) / 2 );
Arrays.sort(arr, (n + 1 ) / 2 , n);
for (i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
}
public static void main(String[] args) {
int [] arr = { 3 , 2 , 7 , 6 , 8 };
int n = arr.length;
odd_even(arr, n);
}
}
|
Python3
def odd_even(arr, n):
i = 1
j = n - 1
if (j % 2 ! = 0 ):
j - = 1
while (i < j):
arr[i], arr[j] = arr[j], arr[i]
i + = 2
j - = 2
arr[: (n + 1 ) / / 2 ] = sorted (arr[: (n + 1 ) / / 2 ])
arr[(n + 1 ) / / 2 :] = sorted (arr[(n + 1 ) / / 2 :])
for i in range (n):
print (arr[i], end = " " )
arr = [ 3 , 2 , 7 , 6 , 8 ]
n = len (arr)
odd_even(arr, n)
|
C#
using System;
class Program
{
static void odd_even( int [] arr, int n)
{
int i = 1;
int j = n - 1;
if (j % 2 != 0)
j--;
while (i < j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i += 2;
j -= 2;
}
Array.Sort(arr, 0, (n + 1) / 2);
Array.Sort(arr, (n + 1) / 2, n - (n + 1) / 2);
for (i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
static void Main( string [] args) {
int [] arr = { 3, 2, 7, 6, 8 };
int n = arr.Length;
odd_even(arr, n);
}
}
|
Javascript
function odd_even(arr, n)
{
let i = 1;
let j = n - 1;
if (j % 2 != 0)
j--;
while (i < j) {
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i += 2;
j -= 2;
}
let temp = arr;
arr = arr.slice(0,Math.floor((n+1)/2)).sort( function (a,b){ return a-b;});
for (let i = Math.floor(n + 1)/2; i < n; i++) arr.push(temp[i]);
for (let i = 0; i < n/2; i++)
console.log(arr[i]);
arr = arr.slice(Math.floor((n+1)/2), n).sort( function (a,b){ return a-b;});
for (let i = 0; i < Math.floor((n)/2); i++)
console.log(arr[i]);
}
let arr = [3, 2, 7, 6, 8];
let n = arr.length;
odd_even(arr, n);
|
Time Complexity: O(n*log(n)) //the inbuilt sort function takes N log N time to complete all operations, hence the overall time taken by the algorithm is N log N
Auxiliary Space: O(1) // since no extra array is used so the space taken by the algorithm is constant
Last Updated :
21 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...