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

  • SEARCH
  • Home
  • 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 8972933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:20:11+00:00 2026-06-15T18:20:11+00:00

Hi I have written a bubble sort application in JAVA. The application is working

  • 0

Hi I have written a bubble sort application in JAVA. The application is working fully and give sufficient output, but it doesn’t give me an out put for average time.

Here is my code:

public class BubbleSort {
static double bestTime = 10000000, worstTime = 0;
public static void main(String[] args) {
    int BubArray[] = new int[]{**#interger values are placed in here#**};
         System.out.println("Unsorted List Before Bubble Sort");
         for(int a = 0; a < BubArray.length; a++){
         System.out.print(BubArray[a] + " ");
            }

        System.out.println("\n Bubble Sort Execution ...");

                    for(int i=0; i<10000;i++) {
                        bubbleSortTimeTaken(BubArray, i);
                        }

         int itrs = bubbleSort(BubArray);
    System.out.println("");               
    System.out.println("Array After Bubble Sort");
    System.out.println("Moves Taken for Sort : " + itrs + " Moves.");
    System.out.println("BestTime: " + bestTime + " WorstTime: " + worstTime);
    System.out.print("Sorted Array: \n");

        for(int a = 0; a < BubArray.length; a++){
                System.out.print(BubArray[a] + " ");
        }
}

private static int bubbleSort(int[] BubArray) {

int z = BubArray.length;
int temp = 0;

int itrs = 0;

for(int a = 0; a < z; a++){
        for(int x=1; x < (z-a); x++){

                if(BubArray[x-1] > BubArray[x]){

                        temp = BubArray[x-1];
                        BubArray[x-1] = BubArray[x];
                        BubArray[x] = temp;


                }    

                itrs++;
        }
}

return itrs;
}

public static void bubbleSortTimeTaken(int[] BubArray, int n) 
{
long startTime = System.nanoTime();
    bubbleSort(BubArray);   
    double timeTaken = (System.nanoTime() - startTime);
    if (timeTaken > 0)
    {
        worstTime = timeTaken;
    }
    else if (timeTaken < bestTime)
    {
        bestTime = timeTaken;
    }

    System.out.println(n + "," + timeTaken);

}
}

But I am unsure of how to a) get the average time for each and also how I would plot the Best, Average and Worst Case results on a graph.

Here is a sample output:

Unsorted List Before Bubble Sort
#Integers Values of Unsorted List#

Bubble Sort Execution ... **#(execution number, time in nanoseconds)#**
0,6336584.0
1,5063668.0
2,3364580.0
3,3373289.0
4,3755912.0
5,3383866.0
....
9995,3431772.0
9996,3368312.0
9997,3743469.0
9998,4639362.0
9999,3433638.0

Moves Taken for Sort : 499500 Moves.
BestTime: 1.0E7 WorstTime: 3433638.0

Also I’m not sure if my bubbleSortTimeTaken() function works properly as 1.0E7 is given for every run of the program regardless of the number of integers used
(100, 1000, 10000, 100000, 1000000) have been tested. I wish to find the average, best and worst case to plot.

Any help would be appreciated, thanks.

  • 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-06-15T18:20:12+00:00Added an answer on June 15, 2026 at 6:20 pm
       if (timeTaken > 0)   // <- PROBLEM, Shouldn't be zero
        {
            worstTime = timeTaken;
        }
        else if (timeTaken < bestTime) //<- PROBLEM! , the 2 comparisons are unrelated
        {
            bestTime = timeTaken;
        }
    

    Right there. If timeTaken>0, which it will be mostly, the else if is never executed.
    Thus, bestTime never gets updated, and retains the original value (1E7).

    To fix this, the function should be modified to look like :

    public static void bubbleSortTimeTaken(int[] BubArray, int n) 
    {
    long startTime = System.nanoTime();
        bubbleSort(BubArray);   
        double timeTaken = (System.nanoTime() - startTime);
        if (timeTaken > worstTime)
        {
            worstTime = timeTaken;
        }
        if (timeTaken < bestTime)
        {
            bestTime = timeTaken;
        }
    
        System.out.println(n + "," + timeTaken);
    
    }
    }
    

    Note that I have added a possible fix about worstTime too.

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

Sidebar

Related Questions

I have written 2 implementation of bubble sort algorithm in C and Haskell. Haskell
i have written a code for insertion sort but it is nt sorting.. kindly
I have written a bubble sort algorithm to sort a linked list. I am
I have written following code for the client of RMI. But getting java.rmi.ConnectException: Connection
I have written a java program generates lots of files (say txt files) in
have written a stochastic simulation in Java, which loads data from a few CSV
I have written a perl code for processing file 'Output.txt' which has below Content.
I have written an MFC dialog based application which is launched by some another
I have written down that Insertion Sort is faster than Selection Sort, which is
Have written some RoR sites in the past, but never bothered too much at

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.