Sort the first and second half of an array Program in java

Question:

Anjali likes to play mathematical tricky games. She gets n numbers for an array. Help Anjali to write a Java program to sort the first half of the array in ascending order and the second half of the array in descending order. If the size of the array is 0 or lesser then display the message as “Array size should be greater than 0”.

Sample Input 1:

Enter the size of an array:

5

Enter the elements:89
23
56
12
99

Sample Output 1:

23

56

89

99

12

Sample Input 2:

Enter the size of an array:

0
Sample Output 2:
Array size should be greater than 0

CODE:

import java.util.*;
public class ArraySort 
{
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        
        System.out.println("Enter the size of an array:");
        int n=sc.nextInt();
        
            if(n>0)
            {
                int[] arr=new int[n];
                System.out.println("Enter the elements:");
                for(int i=0;i<n;i++)
                {
                    arr[i]=sc.nextInt();
                }
                
                int[] arr1=Arrays.copyOfRange(arr,      0,   (n+1)/2);
                int[] arr2=Arrays.copyOfRange(arr,  (n+1)/2,    n   );
                
                Arrays.sort(arr1);
                //Arrays.sort(arr2, Collections.reverseOrder());
                
                for(int i=0;i<n/2;i++)
                {
                    for(int j=0;j<(n/2)-1;j++)
                    {
                        if(arr2[j]<arr2[j+1])
                        {
                            int temp=arr2[j];
                            arr2[j]=arr2[j+1];
                            arr2[j+1]=temp;
                        }
                    }
                }
                
                int len2=arr1.length+arr2.length;
                int[] res=new int[len2];
                
                System.arraycopy(arr1, 0, res,      0,  (n+1)/2 );
                System.arraycopy(arr2, 0, res, (n+1)/2,     n/2   );
                
                for(int i : res)
                {
                    System.out.println(i);
                }
            }
            else
            {
                System.out.println("Array size should be greater than 0");
            }
    }
}
public class Student 
{
    private int id;
    private String name;
    private int[] marks;
    private float average;
    private char grade;
    
    public void setId(int id)
    {
        this.id=id;
    }
    
    public int getId()
    {
        return this.id;
    }
    
    public void setName(String name)
    {
        this.name=name;
    }
    
    public String getName()
    {
        return this.name;
    }
    
    public void setMarks(int[] marks)
    {
        this.marks=marks;
    }
    
    public int[] getMarks()
    {
        return this.marks;
    }
    
    public void calculateAvg()
    {   float sum=0;
        
        for(int i=0;i<this.marks.length;i++)
        {
            sum+=this.marks[i];
        }
        
        average=(float)(sum/(this.marks.length));
        setAverage(average);
    }
    
    public void findGrade()
    {int flag=0;
        for(int i=0;i<this.marks.length;i++)
        {
            if(this.marks[i]<50)
            {
                flag++;
            }
        }
        if(flag>0)
        {
            grade='F';
        }
        else
        {
            if(this.average>=80 && this.average<=100)
            {
                grade='O';
            }
            else if(this.average>=50 && this.average<=79)
            {
                grade='A';
            }
            else
            {
                grade='F';
            }
        }
        setGrade(grade);
        
    }
    
    public void setAverage(float average)
    {
        this.average=average;
    }
    
    public void setGrade(char grade)
    {
        this.grade=grade;
    }
    
    public float getAverage()
    {
        return this.average;
    }
    
    public char getGrade()
    {
        return this.grade;
    }
}
Next:
  1. Retail Shop
  2. Palindrome
  3. Numerology

Leave a Comment

Scroll to Top