Open In App

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

Last Updated : 19 Jul, 2022
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

 



Similar Reads

Sort all even numbers in ascending order and then sort all odd numbers in descending order
Given an array of integers (both odd and even), sort them in such a way that the first part of the array contains odd numbers sorted in descending order, rest portion contains even numbers sorted in ascending order.Examples: Input: arr[] = {1, 2, 3, 5, 4, 7, 10}Output: arr[] = {7, 5, 3, 1, 2, 4, 10} Input: arr[] = {0, 4, 5, 3, 7, 2, 1}Output: arr[]
25 min read
Sort first k values in ascending order and remaining n-k values in descending order
Given an array of size n, arrange the first k elements of the array in ascending order and the remaining n-k elements in descending order. Examples: Input: arr[] = {5, 4, 6, 2, 1, 3, 8, 9, -1}, k = 4 Output: 2 4 5 6 9 8 3 1 -1 Input: arr[] = {5, 4, 6}, k = 2 Output: 4 5 6 Algorithm: Store the first k elements in an array and sort that in ascending
11 min read
Sort an array of strings in ascending order with each string sorted in descending order
Given a array of strings S[] of size N (1 ? N ? 105), sort characters of each string in descending order and then print the array of strings in ascending order. Examples: Input: s[] = {"apple", "box", "cat"} Output: pplea tca xob Explanation: Sorting each string in descending order, S[] modifies to {"pplea", "xob", "tca"}. Sorting the array in asce
9 min read
Sort Matrix in alternating ascending and descending order rowwise
Given an N x N matrix, our task is to print the row of the matrix in ascending and descending order alternatively.Examples: Input: 5 7 3 4 9 5 8 2 6 3 8 1 5 8 9 3 Output: 3 4 5 7 9 8 5 2 1 3 6 8 9 8 5 3 Explanation: Here the first row is sorted in ascending order, second row sorted in descending order, third row sorted in ascending order and so on.
10 min read
Sorting rows of matrix in ascending order followed by columns in descending order
Given a matrix, sort the rows of matrix in ascending order followed by sorting the columns in descending order. Examples : Input : a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Output : 7 8 9 4 5 6 1 2 3 Input : a[3][3] = {{3, 2, 1}, {9, 8, 7}, {6, 5, 4}}; Output : 7 8 9 4 5 6 1 2 3 Approach: Traverse all rows one by one and sort rows in ascending o
10 min read
Sorting rows of matrix in descending order followed by columns in ascending order
Given a matrix of distinct elements. The task is to sort the rows of matrix in descending order followed by sorting the columns in ascending order. Examples: Input: a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Output: 3 2 1 6 5 4 9 8 7 Input: a[3][3] = {{3, 2, 1}, {9, 8, 7}, {6, 5, 4}}; Output: 3 2 1 6 5 4 9 8 7 Approach: Traverse all rows one by o
9 min read
Sum of elements in range L-R where first half and second half is filled with odd and even numbers
Given a number N, create an array such the first half of the array is filled with odd numbers till N, and the second half of the array is filled with even numbers. Also given are L and R indices, the task is to print the sum of elements in the array in the range [L, R]. Examples: Input: N = 12, L = 1, R = 11 Output: 66 The array formed thus is {1,
23 min read
Sort a linked list that is sorted alternating ascending and descending orders?
Given a Linked List. The Linked List is in alternating ascending and descending orders. Sort the list efficiently. Example: Input List: 10 -&gt; 40 -&gt; 53 -&gt; 30 -&gt; 67 -&gt; 12 -&gt; 89 -&gt; NULL Output List: 10 -&gt; 12 -&gt; 30 -&gt; 40 -&gt; 53 -&gt; 67 -&gt; 89 -&gt; NULL Input List: 1 -&gt; 4 -&gt; 3 -&gt; 2 -&gt; 5 -&gt; NULL Output L
13 min read
Merge first half and reversed second half of the linked list alternatively
Given a linked list, the task is to rearrange the linked list in the following manner: Reverse the second half of given linked list. First element of the linked list is the first element of first half.Second element of the linked list is the first element of second half. Examples: Input: 1-&gt;2-&gt;3-&gt;4-&gt;5 Output: 1-&gt;5-&gt;2-&gt;4-&gt;3 I
11 min read
Find the sum of the first half and second half elements of an array
Given an array arr of size N. The task is to find the sum of the first half (N/2) elements and the second half elements (N - N/2) of an array. Examples: Input : arr[] = {20, 30, 60, 10, 25, 15, 40} Output : 110, 90 Sum of first N/2 elements 20 + 30 + 60 is 110Input : arr[] = {50, 35, 20, 15} Output : 85, 35 Approach: Initialize SumFirst and SumSeco
10 min read
Article Tags :
Practice Tags :