Java Program for Sorting first half in Ascending order and second half in Descending order

Sort First half in Ascending and Second half in descending order in Java

Sort First half in Ascending and Second half in descending order in Java

Here, in this page we will discuss the program to sort first half in ascending and second half in descending order in java programming language. We are given with an array and need to print the required sorted array in the desired way.

Methods Discussed in this page :

  • Method 1 : Using bubble sort
  • Method 2 : Sort the entire array then, print first half in ascending and second half in descending.

Method 1 :

This program takes a lot of inspiration from the bubble sort.

Apart from the fact that we divide the array into two halves. In the first half we sort in ascending order and in the second we sort in descending order.

Method 1 : Code in Java

Run
import java.util.*;

class Main
{

   // function to print half of the array in
   // ascending order and the other half in
   // descending order
   static void printOrder(int[] a, int n)
   {
     int temp;
     for(int i=0;i < n-1;i++)
     {
        for(int j = 0;j < n/2; j++) { if(a[j]>a[j+1])
            {
              temp=a[j];
              a[j]=a[j+1];
              a[j+1]=temp;
            }
        }

        for(int j = n/2;j < n-1; j++)
        {
            if(a[j] < a[j+1])
            {
               temp=a[j];
               a[j]=a[j+1];
               a[j+1]=temp;
            }
         }
      }

      for(int i = 0; i < n; i++)
         System.out.print(a[i] + " ");

   }

   // Driver code
   public static void main(String[] args)
   {
       int[] arr = {3, 2, 4, 1, 10, 30, 40, 20};
       int n = arr.length;
       printOrder(arr, n);
    }
}

Output

1 2 3 4 40 30 20 10 

Method 2 :

  • 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.
Sort the array

Method 2 : Code in Java

Run
import java.util.*;

class Main
{

    // 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 = {3, 2, 4, 1, 10, 30, 40, 20};
      int n = arr.length;
      printOrder(arr, n);
   }
}

Output

1 2 3 4 40 30 20 10 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

15 comments on “Java Program for Sorting first half in Ascending order and second half in Descending order”


  • Syed Amjad

    First sort the whole array in ascending order than sort descending from half of the array
    public class Ad {
    public static void main(String[] args) {
    int arr[] = {9,1,8,2,7,3,6,4,5};

    for (int i = 0; i < arr.length; i++) {
    for (int j = i+1; j arr[j]){
    int temp =arr[i];
    arr[i] =arr[j];
    arr[j]=temp;
    }
    }
    }
    System.out.println(Arrays.toString(arr));
    int k = arr.length-1;
    int l = arr.length/2;
    while(l<k){
    int temp = arr[l];
    arr[l]=arr[k];
    arr[k]=temp;
    l++;
    k–;
    }
    System.out.println(Arrays.toString(arr));
    }
    }


  • lalit

    //Sort first half in ascending order and second half in descending :
    public class AscDesc {

    public static void main(String arg[]) {

    int arr[]= {15,75,31,14,65,24};
    int n=arr.length;
    int temp=0;

    int arr1[]= new int[n];

    for(int i=0;i<arr.length;i++) {
    for(int j=i+1;jarr[j]) {
    temp=arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
    }
    }
    }
    for(int i=0;i=n/2;i–) {
    arr1[j]=arr[i];
    j++;
    }
    for(int i=0;i<arr1.length;i++) {
    System.out.print(arr1[i]+" ");
    }
    }
    }


  • Khushi Saxena

    import java.util.*;
    class HelloWorld {
    public static void main(String[] args) {
    int a[]={5,9,3,8,4};
    int n=a.length;
    Arrays.sort(a);
    for(int i=0;i=n/2;j–){
    System.out.println(a[j]);
    }
    }
    }
    Time Complexity- O (nlogn)


  • Ashish Raj

    import java.util.Arrays;

    class A{
    void order(int arr[],int n){
    Arrays.sort(arr);

    int j=0;
    for(int i=n/2;i<=n/2;i++){
    int temp=arr[i];
    arr[i]=arr[n-1-j];
    arr[n-1-j]=temp;
    j++;
    }
    for(int i=0;i<n;i++){
    System.out.println(arr[i]);
    }
    }
    }

    class Main{
    public static void main(String args[]){
    int arr[]={1,45,23,98,66};
    A obj= new A();
    int n=arr.length;
    obj.order(arr,n);
    }
    }


  • Ankitha

    package codes100;

    import java.util.Arrays;
    import java.util.Scanner;

    public class SortAssSortDec {
    public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    int n= sc.nextInt();
    int array[]=new int[n];
    for(int i=0; i<n; i++) {
    array[i]=sc.nextInt();
    }
    Arrays.sort(array);
    for(int i=0; i=n/2; i–) {
    System.out.println(array[i]);
    }

    }

    }


  • Anushka

    public class SortFirstHalfInAscendingAndOtherInDescending {
    public static void main(String[] args) {
    int [] arr = {2,4,6,1,4,8,1,9,2,5};
    sort(arr,arr.length);
    for( int i = 0 ; i < arr.length ; i++ )
    {
    System.out.print(arr[i] + " ");
    }

    }
    public static void sort(int [] arr, int n)
    {
    int p = n/2;
    int l = 0 ; int h = n-1;
    // selection sort o(n^2)

    // ascending

    for(int i = l ; i < p; i++)
    {
    int min = i;
    for(int j = i ; j < p ; j++)
    {
    if(arr[j] < arr[min])
    min = j;
    }
    // swap
    int temp = arr[i];
    arr[i] = arr[min];
    arr[min] = temp;
    }

    // descending
    for(int i = p ; i <= h; i++)
    {
    int max = i;
    for(int j = i ; j arr[max])
    max = j;
    }
    // swap
    int temp = arr[i];
    arr[i] = arr[max];
    arr[max] = temp;
    }

    }
    }


  • Sai

    I think it should be like sorting the values in the array itself (instead of just printing) would make sense..


  • mur4l33

    int m = (arr.length + 1) / 2;
    Arrays.sort(arr);
    for (int i = 0; i = m; i–) {
    System.out.println(arr[i]);
    }


  • mur4l33

    import java.util.Arrays;

    public class Halfascdesc {
    public static void main(String args[]) {
    int[] arr = { 4, 5, 2, 3, 1, 6 };
    int m = (arr.length + 1) / 2;
    Arrays.sort(arr);
    for (int i = 0; i = m; i–) {
    System.out.println(arr[i]);
    }
    }
    }


  • Mahesh

    int[]a={34,223,67,21,212,443,45,677,3224,65,78,54,24,67,4478,345,88,22,43,25,66,11,11,23,23,15};
    Arrays.sort(a);
    int low=0;
    int high=a.length;
    int mid=(high+low)/2;
    for(int i=0;i=mid;i–){
    System.out.print(a[i]+” “);
    }


    • Mahesh

      int[]a={34,223,67,21,212,443,45,677,3224,65,78,54,24,67,4478,345,88,22,43,25,66,11,11,23,23,15};
      Arrays.sort(a);
      int low=0;
      int high=a.length;
      int mid=(high+low)/2;
      for(int i=0;i=mid;i–){
      System.out.print(a[i]+” “);
      }


  • Shubham

    public class JavaApplication1
    {
    public static void main(String[] args)
    {
    Scanner obj = new Scanner(System.in);
    int n;
    n = obj.nextInt();
    int a[] = new int[n];
    for(int i =0;i<n;i++)
    {
    a[i]=obj.nextInt();
    }
    for(int x=0;x<(n+1)/2;x++)
    {
    for(int y=x+1;y<(n+1)/2;y++)
    {
    if(a[y]<a[x])
    {
    int temp;
    temp = a[x];
    a[x]=a[y];
    a[y]=temp;
    }
    }
    }

    for(int p=n/2;p<n;p++)
    {
    for(int q=p+1;q<n;q++)
    {
    if(a[p]<a[q])
    {
    int temp;
    temp = a[p];
    a[p]=a[q];
    a[q]=temp;
    }
    }
    }
    for(int i =0;i<n;i++)
    {
    System.out.print(a[i]+" ");
    }

    }
    }