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.

15 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. Pingback: Google

  8. I precisely wanted to appreciate you all over again. I do not know what I could possibly have accomplished in the absence of those suggestions documented by you regarding such a situation. Certainly was a real distressing difficulty in my opinion, nevertheless encountering your specialized technique you managed that took me to jump for joy. I am just thankful for the service and thus have high hopes you really know what a great job you are always doing educating men and women with the aid of your webpage. I know that you haven’t encountered any of us.

  9. I needed to write you a bit of word to finally say thanks again for these beautiful tactics you have contributed here. It is really pretty open-handed of you to allow extensively what exactly most of us would have sold for an ebook to help with making some profit for their own end, mostly since you might well have done it if you ever desired. The tips also worked as a fantastic way to comprehend someone else have similar zeal similar to my very own to learn a whole lot more related to this issue. I am sure there are several more pleasant moments up front for individuals that read carefully your website.

  10. I in addition to my guys appeared to be following the excellent strategies on your website and so unexpectedly I had a terrible suspicion I had not thanked you for them. The men ended up totally passionate to read them and already have without a doubt been loving them. We appreciate you simply being indeed helpful and also for deciding on variety of tremendous subject areas most people are really desperate to be informed on. My very own sincere regret for not expressing gratitude to you earlier.

  11. I precisely had to appreciate you all over again. I do not know the things I would’ve followed without those opinions documented by you regarding my area of interest. Certainly was a very depressing problem in my view, nevertheless coming across your well-written approach you processed it forced me to leap over fulfillment. Extremely grateful for this assistance as well as pray you comprehend what a great job that you are doing instructing the rest through the use of a blog. I am certain you haven’t met any of us.

Leave a Comment

Your e-mail address will not be published. Required fields are marked *