Open In App

Sort first half in ascending and second half in descending order | 1

Improve
Improve
Like Article
Like
Save
Share
Report

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 : 

  1. Sort the given array. 
  2. Run a loop up to half the length of the array and print the elements of the sorted array. 
  3. 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++




// C++ program to print first half in
// ascending order and the second half
// in descending order
#include <bits/stdc++.h>
using namespace std;
 
// function to print half of the array in
// ascending order and the other half in
// descending order
void printOrder(int arr[], int n)
{
    // sorting the array
    sort(arr, arr + n);
 
    // printing first half in ascending
    // order
    for (int i = 0; i < n / 2; i++)
        cout << arr[i] << " ";   
 
    // printing second half in descending
    // order
    for (int j = n - 1; j >= n / 2; j--)
        cout << arr[j] << " ";    
}
 
// driver code
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




// Java program to print first half in
// ascending order and the second half
// in descending order
import java.util.*;
 
class GFG
{
// function to print half of the array in
// ascending order and the other half in
// descending order
static void printOrder(int[] arr, int n)
{
    // sorting the array
    Arrays.sort(arr);
 
    // printing first half in ascending
    // order
    for (int i = 0; i < n / 2; i++)
        System.out.print(arr[i]+" ");
 
    // printing second half in descending
    // order
    for (int j = n - 1; j >= n / 2; j--)
    System.out.print(arr[j]+" ");
     
}
 
// Driver code
public static void main(String[] args)
{
    int[] arr = { 5, 4, 6, 2, 1, 3, 8, 9, 7 };
    int n = arr.length;
    printOrder(arr, n);
 
}
}
/* This code is contributed by Mr. Somesh Awasthi */


Python




# Python program to print first half in
# ascending order and the second half
# in descending order
 
 
# function to print half of the array in
# ascending order and the other half in
# descending order
def printOrder(arr,n) :
     
    # sorting the array
    arr.sort()
  
    # printing first half in ascending
    # order
    i = 0
    while (i< n/ 2 ) :
        print arr[i],
        i = i + 1
         
    # printing second half in descending
    # order
    j = n - 1
    while j >= n / 2 :
        print arr[j],
        j = j - 1
         
# Driver code
arr = [5, 4, 6, 2, 1, 3, 8, 9, 7]
n = len(arr)
printOrder(arr, n)
 
# This code is contributed by Nikita Tiwari.


C#




// C# program to print first half in
// ascending order and the second half
// in descending order
using System;
 
class GFG {
     
    // function to print half of the array in
    // ascending order and the other half in
    // descending order
    static void printOrder(int[] arr, int n)
    {
         
        // sorting the array
        Array.Sort(arr);
     
        // printing first half in ascending
        // order
        for (int i = 0; i < n / 2; i++)
            Console.Write(arr[i] + " ");
     
        // printing second half in descending
        // order
        for (int j = n - 1; j >= n / 2; j--)
            Console.Write(arr[j] + " ");
    }
     
    // Driver code
    public static void Main()
    {
        int[] arr = { 5, 4, 6, 2, 1, 3, 8, 9, 7 };
        int n = arr.Length;
         
        printOrder(arr, n);
    }
}
 
// This code is contributed by vt_m.


PHP




<?php
// PHP program to print first half in
// ascending order and the second half
// in descending order
 
// function to print half of the array in
// ascending order and the other half in
// descending order
function printOrder($arr, $n)
{
     
    // sorting the array
    sort($arr);
 
    // printing first half
    // in ascending order
    for ($i = 0; $i < intval($n / 2); $i++)
        echo $arr[$i] . " ";
 
    // printing second half
    // in descending order
    for ($j = $n - 1; $j >= intval($n / 2); $j--)
        echo $arr[$j] . " ";    
}
 
    // Driver Code
    $arr = array(5, 4, 6, 2, 1,
                    3, 8, 9, 7);
    $n = count($arr);
    printOrder($arr, $n);
 
// This code is contributed by Sam007
?>


Javascript




<script>
 
// javascript program to print first half in
// ascending order and the second half
// in descending order
 
    // function to print half of the array in
    // ascending order and the other half in
    // descending order
    function printOrder(arr , n)
    {
        // sorting the array
        arr.sort();
 
        // printing first half in ascending
        // order
        for (i = 0; i < parseInt(n / 2); i++)
            document.write(arr[i] + " ");
 
        // printing second half in descending
        // order
        for (j = n - 1; j >= parseInt(n / 2); j--)
            document.write(arr[j] + " ");
 
    }
 
    // Driver code
     
        var arr = [ 5, 4, 6, 2, 1, 3, 8, 9, 7 ];
        var n = arr.length;
        printOrder(arr, n);
 
// This code contributed by aashish1995
 
</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. 

  1. 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).
  2. 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++




// C++ program to print first half in
// ascending order and the second half
// in descending order
#include <bits/stdc++.h>
using namespace std;
 
// function to print half of the array in
// ascending order and the other half in
// descending order
void printOrder(int arr[], int n)
{
    // sorting the array
    sort(arr, arr + n);
 
    // printing first half in ascending
    // order
    for (int i = 0; i < n / 2; i++)
        cout << arr[i] << " ";
 
    // printing second half in descending
    // order
    for (int j = n - 1; j >= n / 2; j--)
        cout << arr[j] << " ";    
}
 
// driver code
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




// Java program to print first half in
// ascending order and the second half
// in descending order
import java.util.*;
 
class GFG
{
 
    // function to print half of the array in
    // ascending order and the other half in
    // descending order
    static void printOrder(int[] arr, int n)
    {
        // sorting the array
        Arrays.sort(arr);
 
        // printing first half in ascending
        // order
        for (int i = 0; i < n / 2; i++)
        {
            System.out.print(arr[i] + " ");
        }
 
        // printing second half in descending
        // order
        for (int j = n - 1; j >= n / 2; j--)
        {
            System.out.print(arr[j] + " ");
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = {5, 4, 6, 2, 1, 3, 8, -1};
        int n = arr.length;
        printOrder(arr, n);
    }
}
 
// This code has been contributed by 29AjayKumar


Python 3




# Python 3 program to print first half
# in ascending order and the second half
# in descending order
 
# function to print half of the array in
# ascending order and the other half in
# descending order
def printOrder(arr, n):
 
    # sorting the array
    arr.sort()
 
    # printing first half in ascending
    # order
    for i in range(n // 2):
        print(arr[i], end = " ")
 
    # printing second half in descending
    # order
    for j in range(n - 1, n // 2 -1, -1) :
        print(arr[j], end = " ")
 
# Driver code
if __name__ == "__main__":
     
    arr = [ 5, 4, 6, 2, 1, 3, 8, -1 ]
    n = len(arr)
    printOrder(arr, n)
 
# This code is contributed by ita_c


C#




// C# program to print first half in
// ascending order and the second half
// in descending order
using System;
 
class GFG
{
     
// function to print half of the array in
// ascending order and the other half in
// descending order
static void printOrder(int[] arr, int n)
{
    // sorting the array
    Array.Sort(arr);
 
    // printing first half in ascending
    // order
    for (int i = 0; i < n / 2; i++)
        Console.Write(arr[i] + " ");
 
    // printing second half in descending
    // order
    for (int j = n - 1; j >= n / 2; j--)
        Console.Write(arr[j] + " ");    
}
 
// Driver code
public static void Main()
{
    int[] arr = {5, 4, 6, 2, 1, 3, 8, -1};
    int n = arr.Length;
    printOrder(arr, n);
}
}
 
// This code is contributed
// by Akanksha Rai


PHP




<?php
// PHP program to print first half in
// ascending order and the second half
// in descending order
 
// function to print half of the array in
// ascending order and the other half in
// descending order
function printOrder($arr, $n)
{
    // sorting the array
    sort($arr);
 
    // printing first half in ascending
    // order
    for ($i = 0; $i < $n / 2; $i++)
        echo $arr[$i] . " ";
 
    // printing second half in descending
    // order
    for ($j = $n - 1; $j >= $n / 2; $j--)
        echo $arr[$j] . " ";    
}
 
// Driver code
$arr = array(5, 4, 6, 2, 1, 3, 8, -1);
$n = sizeof($arr);
printOrder($arr, $n);
 
// This code is contributed
// by Akanksha Rai
?>


Javascript




<script>
 
// Javascript program to print first half in
// ascending order and the second half
// in descending order
 
    // function to print half of the array in
    // ascending order and the other half in
    // descending order
    function printOrder(arr , n) {
        // sorting the array
        arr.sort();
 
        // printing first half in ascending
        // order
        for (i = 0; i < parseInt(n / 2); i++) {
            document.write(arr[i] + " ");
        }
 
        // printing second half in descending
        // order
        for (j = n - 1; j >= parseInt(n / 2); j--) {
            document.write(arr[j] + " ");
        }
    }
 
    // Driver code
     
        var arr = [ 5, 4, 6, 2, 1, 3, 8, -1 ];
        var n = arr.length;
        printOrder(arr, n);
 
// This code contributed by Rajput-Ji
 
</script>


Output: 

-1 1 2 3 8 6 5 4 

 

Time Complexity: O(n logn)
Auxiliary Space: O(1)

Sort first half in ascending and second half in descending order | Set 2

 



Last Updated : 19 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads