Question: Write a program that automatically generates the table of sample run times for the selection sort algorithm. The program should ask for the smallest and

Write a program that automatically generates the table of sample run times for the selection sort algorithm. The program should ask for the smallest and largest value of n and the number of measurements and then make all sample runs.

public class SelectionSorter { public static void sort(int[] a) { for(int i = 0; i < a.length -1; i++) { int minPos = minimumPosition(a,i); ArrayUtil.swap(a, minPos, i); } } private static int minimumPosition(int[] a , int from) { int minPos = from; for(int i = from +1; i < a.length; i++) { if (a[i] < a[minPos]) { minPos = i; } } return minPos; } }

import java.util.Array;

public class SelectionSortDemo { public static void main(String[] args) { int[] a = ArrayUtil.randomIntArray(20, 100); System.out.println(Arrays.toString(a)); SelectionSorter.sort(a); System.out.println(Arrays.toString(a)); } }

import java.util.Random;

public class ArrayUtil { private static Random generator = new Random(); public static int[] randomIntArray(int length, int n) { int [] a = new int[length]; for(int i = 0; i < a.length; i++) { a[i] = generator.nextInt(n); } return a; } public static void swap(int[] a , int i , int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!