Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6987655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:57:56+00:00 2026-05-27T18:57:56+00:00

I sorted an array using an algorithm and when I was going trough a

  • 0

I sorted an array using an algorithm and when I was going trough a book. The algorithm that I wrote had a name (BUBBLE SORT) . I was wondering if the program I roughly wrote implemented the bubble sort algorithm perfectly or is there a more efficient way of doing the same?

import java.util.Arrays;
public class Tool {
    public static void main(String[] args){

        int[] n = {4,8,12,87,32,98,12,45,94,42,938,84,63,67,86,37};
        int inter = 0;

        int arrayLength = n.length;

        int pass = arrayLength-1;
        for(int y = 0; y < arrayLength - 1; y++) {
            for(int x = 0; x < pass; x++) {
                if(n[x] < n[x + 1]) {
                    inter = n[x];
                    n[x] = n[x + 1];
                    n[x + 1] = inter;
                    num++;
                }

            }
            pass--;
        }
        // To print the resulting array
        System.out.println(Arrays.toString(n));
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:57:57+00:00Added an answer on May 27, 2026 at 6:57 pm

    A good website to look for algorithm implementation is http://www.algolist.com. You can find the article on bubble sort here.

    It looks like your code is lacking an optimization that checks if swapping is actually necessary (as Loki Astari noted).

    This code is taken from the above URL, which implements this optimization:

    public void bubbleSort(int[] arr){
        boolean swapped = true;
        int j = 0;
        int tmp;
        while(swapped){
            swapped = false;
            j++;
            for(int i = 0; i < arr.length - j; i++){
                if(arr[i] > arr[i + 1]){
                    tmp = arr[i];
                    arr[i] = arr[i + 1];
                    arr[i + 1] = tmp;
                    swapped = true;
                }
            }
        }
    }
    

    You can read more about it from that page, it has helpful diagrams too if you’re more of a visual learner (like myself).

    Cheers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have a generic bubble sort algorithm which I am using and I
1-)For sorted array I have used Binary Search. We know that the worst case
Having sorted the tag_counts hash via the following code: sorted_tags = Contact.tag_counts.sort{ |x,y| x.name.downcase
Hi I have the following bubble sort algorithm (generic code taken from another site)
i write a c++ code for Bubble sort algorithm and i dont know how
I would like to sort an array in ascending order using C/C++ . The
I have a huge array of ints that I need to sort. The catch
Hi this is my algorithm which takes an array with float numbers that are
I know how to sort an array but I haven't sorted a stack before.
Hi I am using the following insertion sort algorithm and I would like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.