Array Sort (Ascending and Descending Order) In Java

Usually we think that, Ascending and Descending order of array will be very easy and normally we neglect to refresh this program before we going interview. Even though concepts are same Interviewer may ask same program in different way. so we need to be more careful.

One of the most frequently asked question in java interview on java program is arrange the given numbers in sorting order either ascending or descending order. Below programs clearly explains about you need.

1. Arrange Numbers In Ascending Order

Program logic,

  • Get the total number of element count.
  • Get input data and store it in array.
  • Compare each element in array one by one. if 1st element is grater than second element, store 1st element in temp variable and push 2nd element to 1st element position. Then move temp value to 2nd element position.
  • Repeat the same for all elements.
import java.util.Scanner;

public class SortingNumber {

	public static void main(String[] args) {
		//Create Object for Scanner class to get input from keyword
		Scanner scn = new Scanner(System.in);
		
		System.out.print("Enter number of element count: ");
		//Set array size
		int inputValue = scn.nextInt(), temp;	
		
		//Get input from keyword
		System.out.print("Enter array values\n");
		int[] arr = new int[inputValue];	
		
		//Get all input from keyword
		for(int i =0; i<inputValue;i++){				
			arr[i]=scn.nextInt();
		}
		
		//Validate the element for ascending sort order
		for(int m=0;m<arr.length;m++){
			for(int n=m+1;n<arr.length;n++){
				if(arr[m]>arr[n]){
					temp = arr[m];
					arr[m]=arr[n];
					arr[n]=temp;
				}
			}
		}
		
		//Display the sorted order	
		System.out.print("Sorted order are: \n");
		for(int t: arr){
			System.out.println(t);
		}		
	}
}
Output: 
Enter number of element count: 10
Enter array values
22
55
66
99
44
66
11
33
77
55
Sorted order are: 
11
22
33
44
55
55
66
66
77
99

2. Arrange Numbers In Descending Order

Program logic,

  • Get the total number of element count.
  • Get input data and store it in array.
  • Compare each element in array one by one. if 1st element is less than second element, store 1st element in temp variable and push 2nd element to 1st element position. Then move temp value to 2nd element position. (for ascending order)
  • Repeat the same for all elements.
import java.util.Scanner;

public class SortingNumber {

	public static void main(String[] args) {
		//Create Object for Scanner class to get input from keyword
		Scanner scn = new Scanner(System.in);
		
		System.out.print("Enter number of element count: ");
		//Set array size
		int inputValue = scn.nextInt(), temp;	
		
		//Get input from keyword
		System.out.print("Enter array values\n");
		int[] arr = new int[inputValue];	
		
		//Get all input from keyword
		for(int i =0; i<inputValue;i++){				
			arr[i]=scn.nextInt();
		}
		
		//Validate the element for ascending sort order
		for(int m=0;m<arr.length;m++){
			for(int n=m+1;n<arr.length;n++){
				if(arr[m]<arr[n]){
					temp = arr[m];
					arr[m]=arr[n];
					arr[n]=temp;
				}
			}
		}
		
		//Display the sorted order	
		System.out.print("Sorted order are: \n");
		for(int t: arr){
			System.out.println(t);
		}		
	}
}

Output:
Enter number of element count: 10
Enter array values
11 55 99 44 22 33 77 88 66 44 
Sorted order are: 
99
88
77
66
55
44
44
33
22
11

Recommended article for you:

Hey, Now it is your time! Drop a comment if more details needed or if any update requires. Your comments are more valuable to improve our site to help others.

10 thoughts on “Array Sort (Ascending and Descending Order) In Java”

  1. Appreciating the time and energy you put into your site and in depth information you provide. It’s good to come across a blog every once in a while that isn’t the same old rehashed information. Fantastic read! I’ve saved your site and I’m including your RSS feeds to my Google account.

  2. I know this if off topic but I’m looking into starting my own weblog and was curious what all
    is required to get set up? I’m assuming having
    a blog like yours would cost a pretty penny?
    I’m not very web smart so I’m not 100% positive. Any recommendations or advice would be greatly appreciated.
    Many thanks

  3. Hey! I’m at work browsing your blog from my new apple iphone!
    Just wanted to say I love reading through your blog and look forward to
    all your posts! Keep up the great work!

  4. Great blog! Is your theme custom made or did you download it from somewhere?
    A theme like yours with a few simple adjustements would really make my blog
    shine. Please let me know where you got your theme.
    Kudos

  5. Oh my goodness! Impressive article dude! Thank
    you, However I am experiencing issues with your RSS.

    I don’t know the reason why I am unable to subscribe to it.
    Is there anybody else having similar RSS problems? Anyone who knows the solution will you
    kindly respond? Thanks!!

  6. Hello there. I discovered your web site by way of Google at the same time as looking for a related subject, your site came up. It appears to be good. I have bookmarked it in my google bookmarks to come back then.

  7. Thanks a bunch for sharing this with all of us you actually know what you are talking about! Bookmarked. Kindly also visit my web site =). We could have a link exchange arrangement between us!

Leave a Comment

Your email address will not be published.