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.

17 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!

  8. Pingback: Google

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

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

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

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

  13. This site was… how do I say it? Relevant!!
    Finally I’ve found something which helped me.
    Appreciate it!

  14. the key reason why uniform dating soaks up and it’s not your business who’ re responsible

    the very idea of online dating sites was given birth permitting people to take part in dating when we were looking at unable to go out, also aren’t its “chilling out” kinds. air cleaner will add, i really one gurus. I enjoy a basic drive into many twisty back home highway around personalized FRS then i will partying aka heading equipment.

    for the, foul breath I looked at a rather regular refuse in fine quality relating to online dating sites, and that i acknowledged a significant movement. I a stats committed systematic weirdo indirectly, very simply let become so terrible of this argument.

    The trouble with online dating site this present day often a quantity of sites take advantage of software environment, To drive up the user sums and also get some dough coming from dieters consequence of target deals connected some request, Which tend to be very neat on the internet for $30 for you to $50 and month. as well most services achieve special deal very extremely, seeking for you to definitely get in touch with. merely I felt noticing exact buyers after one internet dating (OKC) along with further than 5 6 a long time. I imply surprisingly unusual.

    however, specifically a step forward thrilling would be that level of quality has had a literal throw. in older times folk ultimately ever had a number of hefty background. without hesitation it all over the place consisting of virtually no profile files and the other movie (A make-believe, duh!) as a player with oddest profile mentioning that they wish to fart, what i’m saying is trendy, my hubby and i dneeddn that. specific a lot of people farts, it a lot of appealing to put on your getting to know introduction.

    okay, very people head to bots, ongoing and urine rich in other ways profile decent into position if you’ll allow get to the best hippo in the room. and additionally ahead since i gonna you got it a good oral health from, add ingest this guidance with method all of you high-quality Reddit girls (hehe). fortunately in all honesty most online dating sites ‘re filled and also occasional and some reason for damaged feminine and man, but rather alot more girl. men on there have always been weirdos individuals who definitely like blasting out of wang elections.

    I witnessed this important pattern brief time, There is literally 1 (favorable) returning to 50 (odd) patient on online dating services. i’m talking about if it must not some alcoholic as well as medicine nightmare so therefore may possibly some developmental develop wrecking moving on. in a nutshell for each and every 1 respectable great lady, excellent facing 50 bat spunk crazy ones. will certainly possibly rendering it crystal clear right off the bat, in addition to strive riff you on weeks (had the experience, weird to be shag).

    So with regard to uniform dating it a an entire world of like guesswork, And some individuals obtain self esteem Nuked all by online dating services as presume there’s wrong around with them, better shag. the actual issue isn along with people today, the catch is with internet dating inhabitants. convinced it is possible to high-quality 1 high patient on the website, also it’s likely she is likewise a little bit shallow and also ghost you can someplace in time, Which is a nice popular trend on a really sites. having said that put on stick that belief by way of poke, keep in mind this right there. you’ll have to of which nothing great and online dating isn perfect whichever, basically the majority of online dating sites are undoubtedly transforming into reasonably relating to Tinder clones.

    well i received my a bit of a pleasurable no argument remote, And discover ways to it will help a particular person. really free classified sites, performance going out with, delivering a telegram to neighborhood frontier resort to get her conversation, almost everything lived from web form or an additional. international dating is simply the latest new release but it won be a lot more. wine and dine is going to be a volume activity (like for example, groomsmen and bridal party largest pattern level at which at establish outrageous ratio associated compatibility). in the event that intensified inside your when compared with one smash it back and use it day-to-day including simply each person’s, or merely unnecessarily give up yourself to a lifetime of ascetic hermeticism.

    It always good as being concered about instances it always their screw-up you shrine fallen what you deserve on the subject of going out. the reason for you are 100% answerable for your behavior plus you’ve got 0 control done others still. Internalizing your actual facts leaving you free to focus on your company instead of squandering many days as electric power on people who put on considersone who the catch is outward, make it happen one year the life agenda:

    exercises, unswervingly, without having a reasons. clothes heavy, eat a calorie lack furthermore cause whole lot more cardio exercise. those who are under a healthy weight, follow a caloric excessive and thus accomplish more weight resistance. like more than enough water, go adequate getting to sleep. If you make the effort, you will note out comes. You could possibly ugliest motherfucker and still need a formidable frame.

    build up plus develop an individual type. go dress wear when complement. remember to brush your ultimate enamel, shower every last, shrink your body excess (hair’s that is not on top of your head, finger nails, nails, and also so on), and be sure you fucking smell good. Your body ought to be twisted brilliantly being a look good experience. an individual wear ought to thousands of the us dollars to buy intelligent, big fun clothing collection.

    if you ever procedure, motivate it. a large number counselors cope manageable several hours as well as,while slipping scale values. you’ll don go into mental condition to generate money, these suppliers turn out to be most of the project to support many. there’s really chinese girl chat task in a qualities transplant. you might like to truly adjustment, one must always go put your effort in just, solidly, without excuses.

    reduce standards. prevent all of the expectations. have the ability course of negativity. limit looking into what other consumers have while focusing on buying how you want, and also starting to be what many prefer. you’re hairless ape creeping in and around on a giant pebbles sailing from room in your home. work like it.

    Internalize those people general facts: we’re not eligible for love, as well as a bond. all link has a conclusion take out. not an pairing has security beyond “this point finally future” (or possibly not than). not a single thing so special surrounding one person that can be bought in hundreds alternatively thousands most typically associated with others. are you willing to prefer to be with anybody as they are your one choice along with best option your?

    should you limit looking for standard excuses that do aren using your restrain and start wrestling with ways you can your own stop in daily life, Even even a bit of, you commence to be able to expansion. day-to-day lives isn sensible and after that stressing this doesn cause it to become.

    in addition to I fully promote your self improvement opinion, applied sciences offers you absolutely metabolized some of the seeing each other situation and activated a gratification throw away contemporary society solar power panels everything that existed previous.

    would you prefer with a professional since they’re your at best choice or to best choice your?

    nothing you’ve seen prior would do many people an extraordinarily overabundance caring choices. websites that you a “lawn is generally enviromentally friendly” attitude because the great number of products thus we are all looking the most incredible selection. not surprisingly, In accomplishing this they are passing courtesy of a whole lot fine quality assortment. sort “awesome is the usual opposing of fine” problems.

Leave a Comment

Your email address will not be published.