import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class SelectionSortProblem {
private static void selectionSort(int[] data)
{
int count = 0;
for (int index = 0; index < data.length-1; index++)
{
int min = index;
for (int scan = index + 1; scan < data.length; scan++)
{
count++;
if (data[scan] < data[min])
{
min = scan;
}
}
if (min != index)
{
int temp = data[index];
data[index] = data[min];
data[min] = temp;
}
System.out.println(count);
Count should only be printed one time after the loop is finished. -1
}
}
public static void main(String[] args)
{
int[] array = {10,20,8,2,580,2,50,5,15,2,0,87,56,2,152};
selectionSort(array);
System.out.print("Values after the sort: ");
System.out.println(Arrays.toString(array));
}
}
In java create a method that repeatedly ask the user to enter a positive integer. For each positive integer entered, the method should call the makeRandomArray function from the Sorting code on Scholar to generate an array of that size containing random numbers (in any range you want), sort the array using the modified selectionSort from part (a), and display the number of comparisons taken. Tell the user what to enter to stop the program.
We have added to functions to our class SelectionSortProblem:
In the main() function we call the function askForPositiveNumber().
askForPositiveNumber() runs until the user enters -1.If the user enters a positive number the makeRandomArray(int n) function is called.In makeRandomArray() function we create an array of size entered by the user.we generate random integers using Random class's nextInt() method and add them to our created array.then we return the created array.
then inside the askForPositiveNumber() function we call the SelectionSort function and print the array.
For keeping the count of he comparisons we use a counter variable count which is incremented whenever a comparison is done.we print the count variable at the end of the SelectionSort() function when all the comparisons have been done.
PROGRAM:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.*;
public class SelectionSortProblem {
//function that asks a user to enter a positive no until he enters -1.
private static void askForPositiveNumber() {
//creating Scanner object for taking inputs
Scanner sc = new Scanner(System.in);
//a loop that only terminates when the user enters -1
while (true) {
System.out.println("Enter a positive integer or -1 to stop");
int n = sc.nextInt();
//if input is -1 terminate the loop
if( n == -1 ){
break;
}
int[] arr = makeRandomArray(n);//calling function
selectionSort(arr);//calling function selectionSort()
System.out.println("Sorted Array:");
System.out.println(Arrays.toString(arr));//printing the array
}
}
//function for creating array with random numbers
private static int[] makeRandomArray(int n){
int[] randomArray = new int[n];//array of size n
Random rn = new Random(); //creating Random object
for (int i =0;i<n;i++){
int x = rn.nextInt(100); //create random numbers from 0 to 99 and add it to array
randomArray[i] = x;
}
return randomArray;
}
private static void selectionSort(int[] data)
{
//counter variable for counting comparisons
int count = 0;
for (int index = 0; index < data.length-1; index++)
{
int min = index;
for (int scan = index + 1; scan < data.length; scan++)
{
count++;//incrementing counter variable
if (data[scan] < data[min])
{
min = scan;
}
}
if (min != index)
{
int temp = data[index];
data[index] = data[min];
data[min] = temp;
}
}
System.out.println("No. of comparisons:"+count);//printing no. of comparisons
}
public static void main(String[] args){
askForPositiveNumber();//calling askForPositiveNumber() function
}
}
Modify the selectionSort function from the Sorting code on Scholar to keep track of the total...