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

31 May, 2022
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++
Java
Python
C#
PHP
Javascript
// 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 ordervoid 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 codeint 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 program to print first half in// ascending order and the second half// in descending orderimport java.util.*; class GFG{// function to print half of the array in// ascending order and the other half in// descending orderstatic 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 codepublic 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 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 orderdef 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 codearr = [5, 4, 6, 2, 1, 3, 8, 9, 7]n = len(arr)printOrder(arr, n) # This code is contributed by Nikita Tiwari.
// C# program to print first half in// ascending order and the second half// in descending orderusing 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 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 orderfunction 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?>
<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>
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).  
C++
Java
Python 3
C#
PHP
Javascript
// 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 ordervoid 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 codeint main(){    int arr[] = { 5, 4, 6, 2, 1, 3, 8, -1 };    int n = sizeof(arr) / sizeof(arr[0]);    printOrder(arr, n);     return 0;}
// Java program to print first half in// ascending order and the second half// in descending orderimport 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 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 orderdef 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 codeif __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# program to print first half in// ascending order and the second half// in descending orderusing System; class GFG{     // function to print half of the array in// ascending order and the other half in// descending orderstatic 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 codepublic 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 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 orderfunction 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?>
<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>
-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 2This 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.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
Sam007
techmanager2
Akanksha_Rai
ukasp
29AjayKumar
aashish1995
Rajput-Ji
Kirti_Mangal
singhh3010
samim2000
Amazon
Arrays
Sorting
Amazon
Arrays
Sorting
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.