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

Develop a program that compares the execution time between bubble sort and selec

ID: 3689826 • Letter: D

Question

Develop a program that compares the execution time between bubble sort and selection sort. The program shall be able to read in a file of 1000 numbers. The program shall perform bubble sort on the numbers and record the execution time. Then the program shall perform selection sort on the same test data and record the execution time. On completion the program shall output the sorted numbers to a file and output the execution times to the console.

Explanation / Answer

public class Bubble { private static int[] myArray; private static int[] myBubbleArray; private static int[] mySelectionArray; public static void main(String args[]){ createList(); fillArray(); print("Original Array: ", myArray); long selectionStartTime = System.nanoTime(); selectionSortArray(mySelectionArray); double selectionElapsedTime = (System.nanoTime() - selectionStartTime) / 1000000.0; print("Selection Sorted Array: ", mySelectionArray); System.out.printf("Total execution time for selection sort is %.2f ms ", selectionElapsedTime); long bubbleStartTime = System.nanoTime(); bubbleSortArray(myBubbleArray); double bubbleElapsedTime = (System.nanoTime() - bubbleStartTime) / 1000000.0; print("Bubble Sorted Array: ", myBubbleArray); System.out.printf("Total execution time for bubble sort is %.2f ms ", bubbleElapsedTime); } private static int[] selectionSortArray(int[] array) { int first; int temp; for(int i=array.length -1; i > 0; i--){ first = 0; for(int j = 1; j array[first]) first = j; } temp = array[first]; array[first] = array[i]; array[i] = temp; } return mySelectionArray; } private static int[] bubbleSortArray(int[] array) { boolean swapped = true; int temp; while(swapped){ swapped = false; for(int i = 0; i