Question
Fill in the Java program a bubble sort method AND a selection sort method (EITHER TEMPLATE or array of ints versions) where indicated below and
Fill in the Java program a bubble sort method AND a selection sort method (EITHER TEMPLATE or array of ints versions) where indicated below and run the program. Fill in a table of results.
// Example of timing simple sort algorithms public class Exercise6_2 { public static int MIN_SIZE = 5000; public static int MAX_SIZE = 40000;
// PUT BUBBLE SORT AND SELECTION SORT HERE // --------------- main ---------------
public static void main( String [] args ) { int k, // index for array arraySize; // size of array (changes) long startTime, stopTime; // for timing double elapsedTime = 0; // for timing Integer [] arrayOfInts1 = null; // for dynamic array Integer [] arrayOfInts2 = null; // copy of dynamic array elements
for(arraySize = MIN_SIZE; arraySize <= MAX_SIZE; arraySize*=2) { // allocate array and stuff will values arrayOfInts1 = new Integer[arraySize]; arrayOfInts2 = new Integer[arraySize]; for (k = 0; k < arraySize; k++) { // THIS USES AUTO-BOXING, SO YOU NEED AT LEAST Java 6 FOR THIS arrayOfInts1[k] = arrayOfInts2[k] = (int)(Math.random()*arraySize); } startTime = System.nanoTime(); // start timing bubble sort----------
// sort using a bubble sort bubbleSort(arrayOfInts1);
stopTime = System.nanoTime(); // stop timing --------------------- elapsedTime =(double)(stopTime - startTime)/1000000.0;
System.out.println(" N: " + arraySize + " Bubble Sort Time: " + elapsedTime + " milliseconds.");
startTime = System.nanoTime(); // start timing selection sort-------
// sort using a bubble sort selectionSort(arrayOfInts2);
stopTime = System.nanoTime(); // stop timing ---------------------
elapsedTime =(double)(stopTime - startTime)/1000000.0;
System.out.println(" N: " + arraySize + " Selection Sort Time: " + elapsedTime + " milliseconds."); }// end for
}// end main } // end class Exercise6_2
1. What is the ratio of the timings for the different sizes when doubled (for this question, compare Bubble Sort to Bubble Sort and Selection Sort to Selection Sort, timings of the different sizes)?
2. Do the timing results agree with what you expected? (different sizes AND Bubble Sort vs. Selection Sort)
3. What did you expect and why? Use the Big-Oh of the sorting algorithms in your analysis.
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