Input n numbers of elements into an array and sort them in descending order

Share Your Love

Hello guys, welcome to Lingarajtechhub.com . Today we are going to discus about Input n numbers of elements into an array and sort them in descending order.

 package com.array.java;
         import java.util.Scanner;

         public class Descending_order {

	     public static void main(String[] args) {
	
		int n;//array size declaration
		Scanner Sc=new Scanner(System.in);
		System.out.println("enter the number of elements:");
		n=Sc.nextInt( );
		int arr[] =new int[n];//array declaration
		System.out.println("enter the elements of the array:");
		for(int i=0;i<n;i++)
		{
			arr[i]=Sc.nextInt( );
		}
		int temp=0;//temporary variable to store the element
		for(int i=0;i<arr.length;i++)//holds each array element
		{
		for(int j=i+1;j<arr.length;j++)//compares with remaining array elements
			{
				if(arr[i]<arr[j])//compare and swap
				{
					temp=arr[i];
					arr[i]=arr[j];
					arr[j]=temp;
				}
			}
		}
		System.out.println( );//displaying elements of array after sorting
		for(int i=0;i<arr.length;i++)
		{
			System.out.println(arr[i]+" ");
		}
		
		
		}
		}

Line-1:

 ❖ Package com.array.java;

            It is a java package( com.array.java) which is a collection of classes and this package is created in a java project .

Line-2:

❖ import java.util.Scanner;

             Scanner is a class in java. util package used for obtaining the input of the primitive types like int, double, etc. and strings .

Line-4:

❖ public class Descending_order{

            *Here  class Descending_order is defined as a class which is a template used to create objects and to define object data types and methods .

 >–and it’s defined as public because it can be accessed anywhere .

Line-6:

 ❖ public static void main(String[] args)

             >>– it is a main method in which we can execute a program //public :it is an access modifier of the main function which must be public so that the JVM which is (Java Virtual Machine)can access and execute this method. 

           //If a method is not public, then access is restricted and its showing error //static is a keyword in which main method has must to be static in which JVM can load the class into memory and call the main method without creating an instance

         //void it means no return type

         //main() is function string[]args : string means collection of characters ,String[] args represents a collection of Strings that are separated by a space and can be typed into the program on the terminal directly.

Line-8:

 ❖ int n;

        >>it means we declare the size of array.

Line-9:

 ❖ Scanner sc=new Scanner(System.in);

          >>–it means here scanner is a class and Sc is the instance of the object which is new Scanner.System.in is an argument 

Line-10:

❖ System.out.println(“Enter the number of elements: “);   

          >>– it means System is a class name and out is an instance of the System println is print a new line .

Line-11:

  •  n=sc.nextInt();

         >>– here i is a variable that we take and Sc is our instance name which is created for an object and nextInt() is a method method of a Scanner object that reads in a string of digits or char and converts them into an int type.

Line-12:

 ❖ int arr[ ]=new int[n] ;

        >>– here we declare array size and initialise the array element which is integer type.

Line-13:

❖ System.out.println(“enter the elements of the array”);

                >>– it means System is a class name and out is an instance of the System println is print a new line .

Line-14:

❖ for(int i=0;i<n;i++) {

           >>it means  loop through the array by incrementing the value of i.

Line-16:

❖ arr[i]=Sc.nextInt();

       >>– here i is a variable that we take and Sc is our instance name which is created for an object and nextInt() is a method method of a Scanner object that reads in a string of digits or char and converts them into an int type.

Line-18:

 ❖int temp=0;

     >>temporary variable to store the element.

Line-19:

❖ for(int i=0;i<n;i++) {

           >>it means  loop through the array by incrementing the value of i,and hold each array element.

Line-21:

❖for(int j=i+1;j<arr.length;j++)

         >>it compares the remaining array elements.

Line-23:

❖if(arr[i]<arr[j]

        >>compare and swap.

Line-31:

❖ System.out.println( );

                >>– it means System is a class name and out is an instance of the System println is print a new line .

      >>– displaying elements of array after sorting.

Line-32:

❖for(int i=0;i<arr.length;i++)

         >> >>it means  loop through the array by incrementing the value of i,and hold each array element.

Line-34:

❖ System.out.println(arr[i];i+” “ );

                >>– it means System is a class name and out is an instance of the System println is print a new line .

            >>+ is a comma operator.

Conclusion:

Share Your Love
Avatar photo
SONALIN SAHOO

Myself Sonalin Shaoo. I am quite happy to contribute here some java programming content and my friends Lipsa Mohanty ,Sushil Puhan, Aswin Kumar Nayak also help me to contribute their Opinion on this .

Articles: 40

Newsletter Updates

Enter your email address below to subscribe to our newsletter