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
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