Question
PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java, Write a computer program that prompts the user for
PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!!
3 .Using Java, 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
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 50, 250 and 500 as the size of the array for each of the two algorithms.
4. Repeat 3 using selection sort.
5. Create a spreadsheet showing the results of 3 and 4 and create a graph to graphically represent the information. Show both sort algorithms on the same graph for comparison.
6. Write a one page document explaining the results, bearing in mind that both algorithms have a complexity of O(n^2) and what you know about complexity analysis. Use your knowledge of complexity analysis to explain your results.
THIS IS THE ANSWER TO NUMBER 3 AND 4. PLEASE USE THIS TO ANSWER 5 AND 6***********************************
3.
import java.util.Random; public class MyClassUsingBubbleSort { public static void main(String args[]) { // int n=0; createAndSortArray(50); createAndSortArray(250); createAndSortArray(500); } static void createAndSortArray(int n){ Random rand = new Random(); //int value = rand.nextInt(50); long totalTime=0,startTime=0,endTime=0; long avgTime=0; for(int i=0;i<1000;i++){ int[] arr=new int[n]; for(int j=0;j
4.
import java.util.Random; public class MyClassUsingSelectionSort { public static void main(String args[]) { // int n=0; createAndSortArray(50); createAndSortArray(250); createAndSortArray(500); } static void createAndSortArray(int n){ Random rand = new Random(); //int value = rand.nextInt(50); long totalTime=0,startTime=0,endTime=0; long avgTime=0; for(int i=0;i<1000;i++){ int[] arr=new int[n]; for(int j=0;j for (int i = 0; i < n-1; i++) { int min_idx = i; for (int j = i+1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; int temp = arr[min_idx]; arr[min_idx] = arr[i]; arr[i] = temp; } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started