Question

PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java,...

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;
import java.util.Scanner;

public class Bubblesortiterations //Declare the class
{
public static void main(String args[]) //main method
{
Scanner sc = new Scanner(System.in); //Declare scanner class
System.out.print("Enter number: "); //Prompt and read the input size
int inputSize = sc.nextInt();
System.out.print("Enter number of iterations: ");

int num_iter = sc.nextInt();
int[] array = new int[inputSize]; //Create array of input size

Random rand = new Random(); //Create Random object
for(int i=0;i<inputSize;i++) //Generate the random value
{
array[i] = rand.nextInt(100);
}

System.out.println("Before Bubble sort"); //Print the random values before Bubble sort
for(int i=0;i<inputSize;i++) //Print the input array values
{
System.out.print(array[i]+" ");
}
System.out.println("\nSort the numbers using Bubble sort: ");
int temp,sortedcount = 0; //Declare local variables

long startTime = System.currentTimeMillis(); //Start the time

for (int c = 0 ; c < ( num_iter - 1 ); c++) //using two for-loops sort the input array
{
for (int d = 0 ; d < num_iter - c - 1; d++)
{
if (array[d] > array[d+1])
{
temp = array[d];
array[d] = array[d+1];
array[d+1] = temp;
sortedcount++;
}
}
}

long endTime = System.currentTimeMillis(); //End the time
long totalTime = endTime - startTime; //Compute the total time

System.out.println("Total iterations: "+num_iter); //Print the values
System.out.println("Total numbers sorted : "+sortedcount);
System.out.println("Total time taken: "+totalTime);
System.out.println("After Bubble sort");
for(int i=0;i<inputSize;i++){
System.out.print(array[i]+" ");
}
}
}

4.

import java.util.Random;
import java.util.Scanner;


public class SelectionSortIterations
{
public static void main(String args[]) //main method
{
Scanner sc = new Scanner(System.in); //Declare scanner class
System.out.print("Enter number: "); //Prompt and read the input size
int inputSize = sc.nextInt();
System.out.print("Enter number of iterations: ");
int num_iter = sc.nextInt();

int[] array = new int[inputSize]; //Create array of input size

Random rand = new Random(); //Create Random object
for(int i=0;i<inputSize;i++) //generate the random values
{
array[i] = rand.nextInt(100);
}
System.out.println("Before Selection sort"); //print the random values before Selection sort
for(int i=0;i<inputSize;i++) //print the input array values
{
System.out.print(array[i]+" ");
}
System.out.println("\nSort the numbers using Selection sort: "); //Print the header
int sortedcount = 0;
long startTime = System.currentTimeMillis();
for (int i = 0; i < num_iter-1; i++)
{
int min_index = i;
for (int temp = i+1; temp < inputSize; temp++)
if (array[temp] < array[min_index])
{
min_index = temp;
}

int temp = array[min_index]; // Swap the found minimum element with the first element
array[min_index] = array[i];
array[i] = temp;
sortedcount++;
}
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;

System.out.println("Total iterations: "+num_iter); //print the values
System.out.println("Total numbers sorted : "+sortedcount);
System.out.println("Total time taken: "+totalTime);
for(int i=0;i<inputSize;i++)
{
System.out.print(array[i]+" ");
}
}
}

Need nano seconds, thanks.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer 5: Below are #1

1. Timings in nano seconds for both sorting algorithms

2. Output of the program

3. Chart for comparison

4. Configuration options from Excel

analysing the below Screen shots:

Answer 6.: Analyzing complexity for multiple runs of same program

below Screen shots analysing output

below following the cases

Analysis

1. Numbers of sorting average are varying because of the order of numbers in which they get input, in some case they will be closer to best case and in some cases closer to worst case.

2. Selection sort mostly out performs bubble sort because the number of swaps needed by the two algorithms (bubble sorts needs more swaps)

3. If we're dropping big-oh a little bit, bubble-sort really does do n^2 comparisons+possible swaps, but selection sort does (n^2 + n)/2, which is less than bubblesort for all n > 1

Add a comment
Know the answer?
Add Answer to:
PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi All, Can someone please help me correct the selection sort method in my program. the...

    Hi All, Can someone please help me correct the selection sort method in my program. the output for the first and last sorted integers are wrong compared with the bubble and merge sort. and for the radix i have no idea. See program below import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Sort { static ArrayList<Integer> Data1 = new ArrayList<Integer>(); static ArrayList<Integer> Data2 = new ArrayList<Integer>(); static ArrayList<Integer> Data3 = new ArrayList<Integer>(); static ArrayList<Integer> Data4 = new ArrayList<Integer>(); static int...

  • Write a program that obtains the execution time of selection sort, radix sort, bubble sort, merge...

    Write a program that obtains the execution time of selection sort, radix sort, bubble sort, merge sort, quick sort, and heap sort for input size 50000, 100,000, 150,000, 200,000, 250,000, and 300,000. Your program should create data randomly and print a table like this: In the same program, obtain the execution time of selection sort, radix sort, bubble sort, and heap sort for input size 2,000,000, 3,000,000, 4,000,000, and 5,000,000. (Hint: You can use the code template below to obtain...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • Write a program in Java that obtains the execution time of selection sort, insertion sort, bubble...

    Write a program in Java that obtains the execution time of selection sort, insertion sort, bubble sort, merge sort, quick sort, and radix sort. Your program should test all sort methods for input sizes of 10000, 20000, 30000, 40000, 50000, and 60000. The selection sort, bubble sort, and radix sort should also be tested for input sizes 100000 and 200000. Your program should create the data that is sorted from randomly generated integers and should output the results in a...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • ********** PLEASE PROVIDE IN JAVA & NEW SET OF ALGORITHM For this lab use the program...

    ********** PLEASE PROVIDE IN JAVA & NEW SET OF ALGORITHM For this lab use the program at the bottom to sort an array using selection sort. Write a selection sort to report out the sorted low values: lowest to highest. Write another to report out the sorted high values – highest to lowest. Last but not least, the program should output all the values in the array AND then output the 2 requested sorted outputs. -----> MY OLD PROGRAM BELOW...

  • Objective: in Java Write a program that implements 3 sorting algorithms and times them in real ti...

    Objective: in Java Write a program that implements 3 sorting algorithms and times them in real time. These algorithms will sort Cylinders by their volume. First, download the driver and include it in your project. Write a class Cylinder with the following properties baseRadius: a non-negative number that corresponds to the Cylinder’s base’s radius. height: a non-negative number that corresponds to the Cylinder’s height. Next, write a class Sorter with the following bubbleSort: This static method takes in an array...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • Create a java class that user put two inputs and first input generate n length array...

    Create a java class that user put two inputs and first input generate n length array of randomly generated numbers. and the second input changes that number of multiples in the array into zero. for example, if the user puts 3 and 5 then it generates 34 60 10 and since the second input is 5 then the multiple of 5 eliminates so it generates 34 0 0 here is the main method that I'm going to use class Main...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT