Sort Array such that smallest is at 0th index and next smallest it at last index and so on
Given an array, arr[] of N integers, the task is to rearrange the array elements such that the smallest element is at the 0th position, the second smallest element is at the (N-1)th position, the third smallest element at 1st position, 4th smallest element is at the (N-2)th position, and so on for all integers in arr[].
Examples:
Input: arr[] = {10, 23, 12, 17, 9}
Output: 9 12 23 17 10
Explanation:
The smallest element is 9 which is put in the index 0.
Then the second smallest element is 10 which is put in the last position.
The third smallest element is put in the second position from the start.
The fourth smallest in the second position from the last and so on.
Input: arr[] = {1, 3, 3, 4, 5}
Output: 1 3 5 4 3
Approach: We will be using the Two Pointer Technique. The idea is to iterate from the start marked by variable i to the end marked by variable j of the array alternatively until they meet in the middle and keep updating the minimum values at these indices. Below are the steps:
- Set the element at index i as the minimum value. Iterate over [i, j] and compare each value in this range with arr[i], and if the value in the range is less than arr[i] then swap the value between arr[i] and the current element. Increment the value of i.
- Set the element at index j as the minimum value. Iterate over [i, j] and compare each value in this range with arr[j], and if the value in the range is less than arr[j] then swap the value between arr[j] and the current element. Decrement the value of j.
- Place the smallest element in the ith position at the first iteration and the next smallest element in the jth position. Do this until both i and j point to the same position.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void rearrange( int a[], int N)
{
int i = 0, j = N - 1;
int min = 0, k, x = 0, temp;
while (i < j)
{
for (k = i; k <= j; k++)
{
if (a[k] < a[min])
min = k;
}
if (x % 2 == 0)
{
temp = a[i];
a[i] = a[min];
a[min] = temp;
i++;
min = i;
}
else
{
temp = a[j];
a[j] = a[min];
a[min] = temp;
j--;
min = j;
}
x++;
}
for (i = 0; i < N; i++)
cout << a[i] << " " ;
}
int main()
{
int arr[] = { 1, 3, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
rearrange(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
static void rearrange( int [] a)
{
int i = 0 , j = a.length - 1 ;
int min = 0 , k, x = 0 , temp;
while (i < j) {
for (k = i; k <= j; k++) {
if (a[k] < a[min])
min = k;
}
if (x % 2 == 0 ) {
temp = a[i];
a[i] = a[min];
a[min] = temp;
i++;
min = i;
}
else {
temp = a[j];
a[j] = a[min];
a[min] = temp;
j--;
min = j;
}
x++;
}
for (i = 0 ; i < a.length; i++)
System.out.print(a[i] + " " );
}
public static void main(String[] args)
{
int arr[] = { 1 , 3 , 3 , 4 , 5 };
rearrange(arr);
}
}
|
Python3
def rearrange(a, N):
i = 0
j = N - 1
min = 0
x = 0
while (i < j):
for k in range (i, j + 1 ):
if (a[k] < a[ min ]):
min = k
if (x % 2 = = 0 ):
temp = a[i]
a[i] = a[ min ]
a[ min ] = temp
i + = 1
min = i
else :
temp = a[j]
a[j] = a[ min ]
a[ min ] = temp
j - = 1
min = j
x + = 1
for i in range (N):
print (a[i] ,end = " " )
if __name__ = = "__main__" :
arr = [ 1 , 3 , 3 , 4 , 5 ]
N = len (arr)
rearrange(arr, N)
|
C#
using System;
class GFG{
static void rearrange( int [] a)
{
int i = 0, j = a.Length - 1;
int min = 0, k, x = 0, temp;
while (i < j)
{
for (k = i; k <= j; k++)
{
if (a[k] < a[min])
min = k;
}
if (x % 2 == 0)
{
temp = a[i];
a[i] = a[min];
a[min] = temp;
i++;
min = i;
}
else
{
temp = a[j];
a[j] = a[min];
a[min] = temp;
j--;
min = j;
}
x++;
}
for (i = 0; i < a.Length; i++)
Console.Write(a[i] + " " );
}
public static void Main( string [] args)
{
int []arr = { 1, 3, 3, 4, 5 };
rearrange(arr);
}
}
|
Javascript
<script>
function rearrange(a) {
var i = 0, j = a.length - 1;
var min = 0, k, x = 0, temp;
while (i < j) {
for (k = i; k <= j; k++) {
if (a[k] < a[min])
min = k;
}
if (x % 2 == 0) {
temp = a[i];
a[i] = a[min];
a[min] = temp;
i++;
min = i;
} else {
temp = a[j];
a[j] = a[min];
a[min] = temp;
j--;
min = j;
}
x++;
}
for (i = 0; i < a.length; i++)
document.write(a[i] + " " );
}
var arr = [ 1, 3, 3, 4, 5 ];
rearrange(arr);
</script>
|
Time Complexity: O(N2) since two nested loops are used the time taken by the algorithm to complete all operations is quadratic.
Auxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant
Efficient Approach
First sort the array and then using two pointers we will assign value as required by the question in the newArr array and then assign this newArr array value to the original array.
As mentioned in the question the first element of the new array is the smallest element in the 0th position, so newArr[0] = arr[0] as arr is already sorted, then the second smallest element to (n-1)th position means newArr[n-1] = arr[1], then newArr[1] = arr[2], newArr[n-2] = arr[3], so for this, we use two variable, where small pointing 0th position of newArr and large pointing (n-1)th position of newArr. on every fill-up of value in newArr, we increase small by 1 and decrease large by 1. so this way we achieve newArr as required in the question.
Step-by-step approach:
1. Sort given array arr
2. create n size newArr array
3. small = 0, large = 0
4. iterate from i = 0 to n-1
if(i%2 == 0)
newArr[small] = arr[i]
small++
else
newArr[large] = arr[i]
large--
5. copy all element from newArr to arr
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void rearrangeArray( int arr[], int n) {
sort(arr, arr + n);
int newArr[n];
int small = 0, large = n - 1;
for ( int i = 0; i < n; i++) {
if (i % 2 == 0) {
newArr[small] = arr[i];
small++;
}
else {
newArr[large] = arr[i];
large--;
}
}
for ( int i = 0; i < n; i++) {
arr[i] = newArr[i];
}
}
int main() {
int arr[] = { 1, 3, 3, 4, 5};
int n = sizeof (arr) / sizeof (arr[0]);
rearrangeArray(arr, n);
for ( int i = 0; i < n; i++) {
cout << arr[i] << " " ;
}
cout << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
static void rearrangeArray( int [] arr, int n) {
Arrays.sort(arr);
int [] newArr = new int [n];
int small = 0 , large = n - 1 ;
for ( int i = 0 ; i < n; i++) {
if (i % 2 == 0 ) {
newArr[small] = arr[i];
small++;
}
else {
newArr[large] = arr[i];
large--;
}
}
for ( int i = 0 ; i < n; i++) {
arr[i] = newArr[i];
}
}
public static void main(String[] args) {
int [] arr = { 1 , 3 , 3 , 4 , 5 };
int n = arr.length;
rearrangeArray(arr, n);
for ( int i = 0 ; i < n; i++) {
System.out.print(arr[i] + " " );
}
System.out.println();
}
}
|
Python3
def rearrange_array(arr):
arr.sort()
n = len (arr)
new_arr = [ 0 ] * n
small = 0
large = n - 1
for i in range (n):
if i % 2 = = 0 :
new_arr[small] = arr[i]
small + = 1
else :
new_arr[large] = arr[i]
large - = 1
for i in range (n):
arr[i] = new_arr[i]
return arr
arr = [ 1 , 3 , 3 , 4 , 5 ]
rearranged_arr = rearrange_array(arr)
print (rearranged_arr)
|
C#
using System;
class Program {
static void rearrangeArray( int [] arr, int n)
{
Array.Sort(arr);
int [] newArr = new int [n];
int small = 0, large = n - 1;
for ( int i = 0; i < n; i++) {
if (i % 2 == 0) {
newArr[small] = arr[i];
small++;
}
else {
newArr[large] = arr[i];
large--;
}
}
for ( int i = 0; i < n; i++) {
arr[i] = newArr[i];
}
}
static void Main()
{
int [] arr = { 1, 3, 3, 4, 5 };
int n = arr.Length;
rearrangeArray(arr, n);
for ( int i = 0; i < n; i++) {
Console.Write(arr[i] + " " );
}
Console.WriteLine();
}
}
|
Javascript
function rearrangeArray(arr) {
arr.sort((a, b) => a - b);
const newArr = new Array(arr.length);
let small = 0, large = arr.length - 1;
for (let i = 0; i < arr.length; i++) {
if (i % 2 === 0) {
newArr[small] = arr[i];
small++;
}
else {
newArr[large] = arr[i];
large--;
}
}
for (let i = 0; i < arr.length; i++) {
arr[i] = newArr[i];
}
}
const arr = [1, 3, 3, 4, 5];
rearrangeArray(arr);
console.log(arr.join( " " ));
|
Time Complexity: O(N*log(N)) sort operation take nlog(n) time.
Auxiliary Space: O(N) since we use extra temp array
Last Updated :
14 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...