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

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

ID: 3701235 • Letter: 4

Question

4) 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 selection 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 selection sort program I have for it:

import java.util.Random;

import java.util.Scanner;

class SelectSort {

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

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

}

//printing data

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

for(int i=0;i

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

}

System.out.println(" After sorting using selection sort: ");

int temp,j;

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

temp = arr[i];

j = i-1;

while (j >= 0 && arr[j] > temp){

arr[j+1] = arr[j];

j = j-1;

}

arr[j+1] = temp;

}

//after selection sort

for(int i=0;i

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

}

}

}

Explanation / Answer

import java.util.*;

class SelectionSort

{

    public static void selection_sort(int[] a)

    {

        int i, j, min, x;

       

        // traverse the array

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

        {

            // store the index of the smallest element

            min = i;

           

            // traverse the array from the element after i till the end

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

            {

                // if the current element is smaller than the min element

                if (a[j] < a[min])

                    min = j;

            }

            // Swap first element and min

            x = a[min];

            a[min] = a[i];

            a[i] = 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

            selection_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 too 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