How to Sort an Array using Java (Swapping the array)

How to Sort an Array using Java (Swapping the array)

Please visit my YouTube channel to watch more.

This article teaches you how to Sort an Array. This is a very popular interview question as well. This is the code.

 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
package CodeTest;

 * @author bhanuka
public class SortArray {

    public static void main(String[] args) {
        int myArr[] = {100, 45, 2, 32, 5, 75, 42, 32};
        int temp;

        for (int i = 1; i < myArr.length; i++) {
            for (int j = i; j > 0; j--) {

                if (myArr[j] < myArr[j - 1]) {
                    temp = myArr[j];
                    myArr[j] = myArr[j - 1];
                    myArr[j - 1] = temp;
        for (int i = 0; i < myArr.length; i++) {


Please refer to the GIT location if you have any trouble.
You can find more Java interview question examples from here.

30 Replies to “How to Sort an Array using Java (Swapping the array)”

  1. Nice blog! Is your theme custom made or did you download it from somewhere?
    A design like yours with a few simple tweeks would really make my blog
    shine. Please let me know where you got your theme. Thanks
    a lot

  2. Hey there! Do you use Twitter? I’d like to follow you if that would be ok.
    I’m definitely enjoying your blog and look forward to new posts.

  3. hello!,I really like your writing so a lot! proportion we be in contact extra approximately your post
    on AOL? I require a specialist on this house to unravel my problem.
    Maybe that is you! Having a look forward to peer you.

  4. Ahaa, its pleasant conversation about this piece
    of writing at this place at this webpage, I have read all that, so at this
    time me also commenting here.

  5. Pretty nice post. I simply stumbled upon your blog and wished to say that I
    have really loved surfing around your weblog posts. After all I’ll
    be subscribing on your feed and I’m hoping you write once more soon!

  6. Hi! This is my first visit to your blog! We are a team of
    volunteers and starting a new initiative in a community in the same niche.

    Your blog provided us beneficial information to
    work on. You have done a extraordinary job!

  7. I and my buddies have already been digesting the best recommendations from your web blog and at once developed an awful suspicion I had not thanked you for those secrets. The guys are actually for this reason thrilled to study all of them and have in effect in actuality been enjoying them. Thanks for simply being really helpful and for selecting certain cool subject areas most people are really wanting to be informed on. My honest apologies for not expressing gratitude to you earlier.

  8. Hey there! I realize this is somewhat off-topic but
    I needed to ask. Does running a well-established website like yours require a large amount of work?
    I am completely new to running a blog however I do write in my diary every day.
    I’d like to start a blog so I will be able to share
    my own experience and thoughts online. Please
    let me know if you have any suggestions or tips for new aspiring bloggers.
    Appreciate it!

  9. I simply had to appreciate you all over again. I’m not certain what I would’ve achieved in the absence of these advice discussed by you about that question. Certainly was a very fearsome situation in my position, but seeing this expert mode you managed the issue took me to cry for fulfillment. Extremely happy for your service as well as wish you are aware of a great job you have been providing educating the mediocre ones all through your webpage. I know that you haven’t got to know any of us.

  10. I have been surfing online more than three hours today,
    yet I never found any interesting article like yours. It’s pretty worth enough
    for me. In my view, if all site owners and bloggers
    made good content as you did, the web will be a
    lot more useful than ever before.

  11. An fascinating dialogue is value comment. I believe that you must write more on this subject, it won’t be a taboo topic however typically individuals are not sufficient to talk on such topics. To the next. Cheers

  12. Hey there! Would you mind if I share your blog with my zynga group?
    There’s a lot of folks that I think would really enjoy your content.
    Please let me know. Thanks

  13. Like!! I blog quite often and I genuinely thank you for your information. The article has truly peaked my interest.

  14. I was very pleased to search out this internet-site.I wished to thanks to your time for this glorious learn!! I definitely having fun with each little bit of it and I’ve you bookmarked to check out new stuff you blog post.

  15. There are some fascinating closing dates in this article but I don抰 know if I see all of them center to heart. There may be some validity however I’ll take maintain opinion until I look into it further. Good article , thanks and we want more! Added to FeedBurner as well

  16. Spot on with this write-up, I really suppose this web site wants far more consideration. I抣l in all probability be again to read much more, thanks for that info.

  17. A formidable share, I just given this onto a colleague who was doing a bit of analysis on this. And he in reality bought me breakfast as a result of I found it for him.. smile. So let me reword that: Thnx for the deal with! However yeah Thnkx for spending the time to discuss this, I really feel strongly about it and love studying extra on this topic. If attainable, as you develop into expertise, would you mind updating your blog with more details? It’s extremely useful for me. Big thumb up for this weblog publish!

  18. I have been browsing on-line greater than three hours as
    of late, but I by no means discovered any interesting article like yours.

    It’s beautiful worth enough for me. In my view,
    if all web owners and bloggers made just right content as you did, the internet will probably be a
    lot more helpful than ever before.

  19. Greetings from Florida! I’m bored at work so I decided to
    browse your site on my iphone during lunch break.
    I enjoy the knowledge you provide here and can’t wait to take
    a look when I get home. I’m shocked at how fast your blog loaded on my
    phone .. I’m not even using WIFI, just 3G .. Anyhow,
    fantastic site!

  20. Long time supporter, and thought I’d drop a comment.

    Your wordpress site is very sleek – hope you don’t mind me asking what theme you’re using?

    (and don’t mind if I steal it? :P)

    I just launched my site –also built in wordpress like yours– but the theme slows (!) the site down quite a bit.

    In case you have a minute, you can find it by searching for “royal cbd” on Google (would appreciate any feedback) –
    it’s still in the works.

    Keep up the good work– and hope you all take
    care of yourself during the coronavirus scare!