# 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

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.

• 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

```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.

### Method 2 : Code in Java

```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)
{
// 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
```

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

• 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]+" ");
}

}
}