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

3) Write a computer program that prompts the user for one number, n for the numb

ID: 3701234 • Letter: 3

Question

3) Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following:

-Initiate a variable running_time to 0

-Create a for loop that iterates 1000 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 the time (Important: Do not start the timer until after the array is created).

-Use bubble sort to sort the array

-Get the time and set this to end-time Subtract start-time from end-time and add the result to total_time

Once the program has run, note

-The number of items sorted

-The average running time for each array (total_time/1000)

-Repeat the process six times, using 500, 2500 and 5000 as the size of the array for each of the two algorithms.

This is the bubble sort program I have for it:

import java.util.Random;

import java.util.Scanner;

class BubbleSort {

public static void main(String args[]){

Scanner sc = new Scanner(System.in);

System.out.println("Enter number: ");

int n = sc.nextInt();

int[] arr = new int[n];

Random r = new Random();

//filling random numbers

for(int i=0;i<n;i++){

arr[i] = r.nextInt(100);

}

//printing data

System.out.println("Before Bubble sort");

for(int i=0;i<n;i++){

System.out.print(arr[i]+" ");

}

System.out.print(" ");

int temp;

//bubble sort

for (int c = 0 ; c < ( n - 1 ); c++){

for (int d = 0 ; d < n - c - 1; d++){

if (arr[d] > arr[d+1]){

temp = arr[d];

arr[d] = arr[d+1];

arr[d+1] = temp;

}

}

}

//after bubble sort

System.out.println("After Bubble sort");

for(int i=0;i<n;i++){

System.out.print(arr[i]+" ");

}

}

}

Explanation / Answer

import java.util.*;

class BubbleSort

{

    // function to sort array a using bubble sort

    public static void bubble_sort(int a[])

    {

        int i, j, x;

       

        for( i = 0 ; i < a.length - 1 ; i++)

        {

            for( j = 0 ; j < a.length - i - 1 ; ++j)

            {

                if (a[ j + 1 ] < a[ j ])

                {

                    // swap the j and j + 1 th element of a

                    x = a[j];

                    a[ j ] = a[ j + 1 ];

                    a[ j + 1 ] = x;

                }

            }

        }

    }

    public static void display(int a[])

    {

        int n = a.length;

        for (int i=0; i<n; ++i)

            System.out.print(a[i] + " ");

        System.out.println();

    }

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        int k;

       

        long total_time = 0;

       

        System.out.print("Enter the size of array : ");

        int maxSize = sc.nextInt();

       

        for( k = 1 ; k <= 1000 ; k++ )

        {

            // initialize the array

            int[] a = new int[maxSize];

           

            // fill the array with random elements

            for(int j = 0; j < maxSize; j++)

            {

                int n = (int)( java.lang.Math.random()*(maxSize-1) );

                a[j] = n;

            }

           

            int i = 0, j = 0;

            long x;

           

            //printing data

            System.out.println("Before Bubble sort");

            display(a);

           

            // get the current time

            long start = System.currentTimeMillis();

           

            // sort using bubble sort

            bubble_sort(a);

           

            // get the current time

            long end = System.currentTimeMillis();

           

            // calculate time taken to sort

            long time_taken = end - start;

           

            System.out.println("Before Bubble sort");

            display(a);

           

            total_time += time_taken;

           

            //System.out.println("Time Taken : " + time_taken);

           

            //display(a);

        }

       

        System.out.println(" Average Time : " + ( total_time / 1000.0 ) );

    }

}

// Output is very long to be included in the solution.

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