Given an array of integers, the task is to print the array in the order – smallest number, the Largest number, 2nd smallest number, 2nd largest number, 3rd smallest number, 3rd largest number, and so on…..
Examples:
Input : arr[] = [5, 8, 1, 4, 2, 9, 3, 7, 6]
Output :arr[] = {1, 9, 2, 8, 3, 7, 4, 6, 5}
Input : arr[] = [1, 2, 3, 4]
Output :arr[] = {1, 4, 2, 3}
A simple solution is to first find the smallest element and swap it with the first element. Then find the largest element, swap it with the second element, and so on. The time complexity of this solution is O(n2).
An efficient solution is to use sorting.
- Sort the elements of the array.
- Take two variables say i and j and point them to the first and last index of the array respectively.
- Now run a loop and store the elements in the array one by one by incrementing i and decrementing j.
Let’s take an array with input 5, 8, 1, 4, 2, 9, 3, 7, 6 and sort them so the array becomes 1, 2, 3, 4, 5, 6, 7, 8, 9. Now take two variables to say i and j and point them to the first and last index of the array respectively, run a loop, and store the value into a new array by incrementing I and decrementing j.get that the final result as 1 9 2 8 3 7 4 6 5.
The flowchart is as follows:

flowchart- rearrangearray
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void rearrangeArray( int arr[], int n)
{
sort(arr, arr + n);
int tempArr[n];
int ArrIndex = 0;
for ( int i = 0, j = n - 1; i <= n / 2 || j > n / 2;
i++, j--) {
tempArr[ArrIndex] = arr[i];
ArrIndex++;
tempArr[ArrIndex] = arr[j];
ArrIndex++;
}
for ( int i = 0; i < n; i++)
arr[i] = tempArr[i];
}
int main()
{
int arr[] = { 5, 8, 1, 4, 2, 9, 3, 7, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
rearrangeArray(arr, n);
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
static void rearrangeArray( int arr[], int n)
{
Arrays.sort(arr);
int [] tempArr = new int [n];
int ArrIndex = 0 ;
for ( int i = 0 , j = n- 1 ; i <= n / 2 || j > n / 2 ;
i++, j--) {
if (ArrIndex < n)
{
tempArr[ArrIndex] = arr[i];
ArrIndex++;
}
if (ArrIndex < n)
{
tempArr[ArrIndex] = arr[j];
ArrIndex++;
}
}
for ( int i = 0 ; i < n; i++)
arr[i] = tempArr[i];
}
public static void main(String args[])
{
int arr[] = { 5 , 8 , 1 , 4 , 2 , 9 , 3 , 7 , 6 };
int n = arr.length;
rearrangeArray(arr, n);
for ( int i = 0 ; i < n; i++)
System.out.print(arr[i]+ " " );
}
}
|
Python3
def rearrangeArray(arr, n) :
arr.sort()
tempArr = [ 0 ] * (n + 1 )
ArrIndex = 0
i = 0
j = n - 1
while (i < = n / / 2 or j > n / / 2 ) :
tempArr[ArrIndex] = arr[i]
ArrIndex = ArrIndex + 1
tempArr[ArrIndex] = arr[j]
ArrIndex = ArrIndex + 1
i = i + 1
j = j - 1
for i in range ( 0 , n) :
arr[i] = tempArr[i]
arr = [ 5 , 8 , 1 , 4 , 2 , 9 , 3 , 7 , 6 ]
n = len (arr)
rearrangeArray(arr, n)
for i in range ( 0 , n) :
print ( arr[i], end = " " )
|
C#
using System;
public class GFG {
static void rearrangeArray( int []arr, int n)
{
Array.Sort(arr);
int []tempArr = new int [n];
int ArrIndex = 0;
for ( int i = 0, j = n-1; i <= n / 2
|| j > n / 2; i++, j--) {
if (ArrIndex < n)
{
tempArr[ArrIndex] = arr[i];
ArrIndex++;
}
if (ArrIndex < n)
{
tempArr[ArrIndex] = arr[j];
ArrIndex++;
}
}
for ( int i = 0; i < n; i++)
arr[i] = tempArr[i];
}
public static void Main(String []args)
{
int []arr = {5, 8, 1, 4, 2, 9, 3, 7, 6};
int n = arr.Length;
rearrangeArray(arr, n);
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
}
|
PHP
<?php
function rearrangeArray( $arr , $n )
{
sort( $arr );
$tempArr = array ( $n );
$ArrIndex = 0;
for ( $i = 0, $j = $n - 1;
$i <= $n / 2 || $j > $n / 2;
$i ++, $j --)
{
$tempArr [ $ArrIndex ] = $arr [ $i ];
$ArrIndex ++;
$tempArr [ $ArrIndex ] = $arr [ $j ];
$ArrIndex ++;
}
for ( $i = 0; $i < $n ; $i ++)
$arr [ $i ] = $tempArr [ $i ];
for ( $i = 0; $i < $n ; $i ++)
echo $arr [ $i ] . " " ;
}
$arr = array (5, 8, 1, 4, 2,
9, 3, 7, 6 );
$n = count ( $arr ) ;
rearrangeArray( $arr , $n );
?>
|
Javascript
<script>
function rearrangeArray(arr, n)
{
arr.sort();
let tempArr = new Array(n);
let ArrIndex = 0;
for (let i = 0, j = n-1; i <= n / 2
|| j > n / 2; i++, j--) {
if (ArrIndex < n)
{
tempArr[ArrIndex] = arr[i];
ArrIndex++;
}
if (ArrIndex < n)
{
tempArr[ArrIndex] = arr[j];
ArrIndex++;
}
}
for (let i = 0; i < n; i++)
arr[i] = tempArr[i];
}
let arr =[5, 8, 1, 4, 2, 9, 3, 7, 6]
let n = arr.length;
rearrangeArray(arr, n);
for (let i = 0; i < n; i++)
document.write(arr[i] + " " );
</script>
|
Time Complexity: O(n log n),
Auxiliary Space: O(n), since n extra space has been taken.
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 :
21 Aug, 2022
Like Article
Save Article