Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java: Write a computer program that prompts the user for two numbers, n for t

ID: 664124 • Letter: I

Question

In Java:

Write a computer program that prompts the user for two numbers, n for the number of items in the array to sort, and num_ifor a number of iterations. Then do the following:

Initiate a variable running_time to 0

Create a for loop that iterates num_i times.

In the body of the loop,

Create an array of n random integers

Get the time and set this to start-time. You will have to figure out what the appropriate command is in the programming language you are using to find them time

Use sophisticated bubble sort to sort the array

Get the time and set this to end-time

Subtract end-time from start-time and add the result to running_time

Once the program has run, note

The number of items sorted

The number of iterations

The running time

Repeat the process nine times, using 50, 100 and 500 as the size of the array, and 100, 1000 and 10,000 as the number of iterations.

Explanation / Answer

public class BubbleSort {
    public static void main(String[] args) {
        int BubArray[] = new int[10000];
       Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        System.out.println("Array Before Bubble Sort");
            for(int a = 0; a < num; a++){
                 System.out.print(BubArray[a] + " ");
            }

            double timeTaken = bubbleSortTimeTaken(BubArray);
                 bubbleSort(BubArray);
            System.out.println("");             
            System.out.println("Array After Bubble Sort");
           System.out.println("    Time taken for Sort : " + timeTaken + " milliseconds.");
            for(int a = 0; a < BubArray.length; a++){
                System.out.print(BubArray[a] + " ");
            }
    }

private static void bubbleSort(int[] BubArray) {

            int z = BubArray.length;
            int temp = 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;

                            }    
                    }
            }
    }
public static double bubbleSortTimeTaken(int[] BubArray) {
    long startTime = System.nanoTime();
        bubbleSort(BubArray);
    long timeTaken = System.nanoTime() - startTime;
    return timeTaken;
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote