Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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]+" ");

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Distributed Relational Database Architecture Connectivity Guide

Authors: Teresa Hopper

4th Edition

0133983064, 978-0133983067

More Books

Students also viewed these Databases questions

Question

Describe the four techniques for allocating resources.

Answered: 1 week ago

Question

When is a table in BCNF?

Answered: 1 week ago

Question

Discuss briefly the advantages and disadvantages of a CFD contract.

Answered: 1 week ago