Computer Science, asked by banikpiyush9, 7 hours ago

SET A) take n elements as input for sorting, sort the array in such a way that first n/2 values is in ascending order and second half of the array is sorted in descending order. Ex: Input: 12 4 15 18 91 51 output: 4 12 15 91 51 18

Answers

Answered by sumityadav88303
1

Answer:

4,12, 15,18,51,91

output : 91 ,51,18,15,12,4

Answered by shilpa85475
0

Java program to print first half in  ascending order and the second half  in descending order.

Input: 12 4 15 18 91 51

output: 4 12 15 91 51 18

Explanation:

import java.util.*;

 

class abc

{

// function printorder

   

   static void printOrder(int[] arr, int n)

   {

       // sorting the array

       Arrays.sort(arr);

 

       //sort ascending half array

       for (int i = 0; i < n / 2; i++)

       {

           System.out.print(arr[i] + " ");

       }

 

      //sort descending half array

       for (int j = n - 1; j >= n / 2; j--)

       {

           System.out.print(arr[j] + " ");

       }

   }

 

 

   public static void main(String[] args)

   {

       int[] arr = {12,4,14,18,91,51};

       int n = arr.length;

       printOrder(arr, n);

   }

}

Similar questions