Given an array of integers, sort the first half of the array in ascending order and second half in descending order.
Examples:
Input : arr[] = {5, 2, 4, 7, 9, 3, 1, 6, 8}
Output : arr[] = {1, 2, 3, 4, 9, 8, 7, 6, 5}
Input : arr[] = {1, 2, 3, 4, 5, 6}
Output : arr[] = {1, 2, 3, 6, 5, 4}
Algorithm :
- Sort the given array.
- Run a loop up to half the length of the array and print the elements of the sorted array.
- Run a loop from the last index of the array to the middle of the array and print the elements in reverse order.
Below is the implementation for the same.
C++
#include <bits/stdc++.h>
using namespace std;
void printOrder( int arr[], int n)
{
sort(arr, arr + n);
for ( int i = 0; i < n / 2; i++)
cout << arr[i] << " " ;
for ( int j = n - 1; j >= n / 2; j--)
cout << arr[j] << " " ;
}
int main()
{
int arr[] = { 5, 4, 6, 2, 1, 3, 8, 9, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
printOrder(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void printOrder( int [] arr, int n)
{
Arrays.sort(arr);
for ( int i = 0 ; i < n / 2 ; i++)
System.out.print(arr[i]+ " " );
for ( int j = n - 1 ; j >= n / 2 ; j--)
System.out.print(arr[j]+ " " );
}
public static void main(String[] args)
{
int [] arr = { 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , 7 };
int n = arr.length;
printOrder(arr, n);
}
}
|
Python
def printOrder(arr,n) :
arr.sort()
i = 0
while (i< n / 2 ) :
print arr[i],
i = i + 1
j = n - 1
while j > = n / 2 :
print arr[j],
j = j - 1
arr = [ 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , 7 ]
n = len (arr)
printOrder(arr, n)
|
C#
using System;
class GFG {
static void printOrder( int [] arr, int n)
{
Array.Sort(arr);
for ( int i = 0; i < n / 2; i++)
Console.Write(arr[i] + " " );
for ( int j = n - 1; j >= n / 2; j--)
Console.Write(arr[j] + " " );
}
public static void Main()
{
int [] arr = { 5, 4, 6, 2, 1, 3, 8, 9, 7 };
int n = arr.Length;
printOrder(arr, n);
}
}
|
PHP
<?php
function printOrder( $arr , $n )
{
sort( $arr );
for ( $i = 0; $i < intval ( $n / 2); $i ++)
echo $arr [ $i ] . " " ;
for ( $j = $n - 1; $j >= intval ( $n / 2); $j --)
echo $arr [ $j ] . " " ;
}
$arr = array (5, 4, 6, 2, 1,
3, 8, 9, 7);
$n = count ( $arr );
printOrder( $arr , $n );
?>
|
Javascript
<script>
function printOrder(arr , n)
{
arr.sort();
for (i = 0; i < parseInt(n / 2); i++)
document.write(arr[i] + " " );
for (j = n - 1; j >= parseInt(n / 2); j--)
document.write(arr[j] + " " );
}
var arr = [ 5, 4, 6, 2, 1, 3, 8, 9, 7 ];
var n = arr.length;
printOrder(arr, n);
</script>
|
Output: 1 2 3 4 9 8 7 6 5
Time Complexity: O(n logn), where n represents the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Alternate Solution: Here, The elements in the 1st half of the array will remain in 1st half but in ascending order among the elements in the 1st half, and those in 2nd half of array will remain in 2nd half but in descending order among the elements in the 2nd half.
- Sort the 1st half of the array in ascending order just because only the elements in the 1st half of the input array needs to be sorted in ascending order (In this way the original elements in the 1st half of the array will remain in the 1st half but in ascending order).
- Sort the 2nd half of the array in descending order just because only the elements in the 2nd half of the input array needs to be sorted in descending order (In this way the original elements in the 2nd half of the array will remain in the 1st half but in descending order).
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void printOrder( int arr[], int n)
{
sort(arr, arr + n);
for ( int i = 0; i < n / 2; i++)
cout << arr[i] << " " ;
for ( int j = n - 1; j >= n / 2; j--)
cout << arr[j] << " " ;
}
int main()
{
int arr[] = { 5, 4, 6, 2, 1, 3, 8, -1 };
int n = sizeof (arr) / sizeof (arr[0]);
printOrder(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void printOrder( int [] arr, int n)
{
Arrays.sort(arr);
for ( int i = 0 ; i < n / 2 ; i++)
{
System.out.print(arr[i] + " " );
}
for ( int j = n - 1 ; j >= n / 2 ; j--)
{
System.out.print(arr[j] + " " );
}
}
public static void main(String[] args)
{
int [] arr = { 5 , 4 , 6 , 2 , 1 , 3 , 8 , - 1 };
int n = arr.length;
printOrder(arr, n);
}
}
|
Python 3
def printOrder(arr, n):
arr.sort()
for i in range (n / / 2 ):
print (arr[i], end = " " )
for j in range (n - 1 , n / / 2 - 1 , - 1 ) :
print (arr[j], end = " " )
if __name__ = = "__main__" :
arr = [ 5 , 4 , 6 , 2 , 1 , 3 , 8 , - 1 ]
n = len (arr)
printOrder(arr, n)
|
C#
using System;
class GFG
{
static void printOrder( int [] arr, int n)
{
Array.Sort(arr);
for ( int i = 0; i < n / 2; i++)
Console.Write(arr[i] + " " );
for ( int j = n - 1; j >= n / 2; j--)
Console.Write(arr[j] + " " );
}
public static void Main()
{
int [] arr = {5, 4, 6, 2, 1, 3, 8, -1};
int n = arr.Length;
printOrder(arr, n);
}
}
|
PHP
<?php
function printOrder( $arr , $n )
{
sort( $arr );
for ( $i = 0; $i < $n / 2; $i ++)
echo $arr [ $i ] . " " ;
for ( $j = $n - 1; $j >= $n / 2; $j --)
echo $arr [ $j ] . " " ;
}
$arr = array (5, 4, 6, 2, 1, 3, 8, -1);
$n = sizeof( $arr );
printOrder( $arr , $n );
?>
|
Javascript
<script>
function printOrder(arr , n) {
arr.sort();
for (i = 0; i < parseInt(n / 2); i++) {
document.write(arr[i] + " " );
}
for (j = n - 1; j >= parseInt(n / 2); j--) {
document.write(arr[j] + " " );
}
}
var arr = [ 5, 4, 6, 2, 1, 3, 8, -1 ];
var n = arr.length;
printOrder(arr, n);
</script>
|
Time Complexity: O(n logn)
Auxiliary Space: O(1)
Sort first half in ascending and second half in descending order | Set 2
This article is contributed by Ayush Saxena. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.