Answer:
Step-by-step explanation:
Here is a possible implementation of the JavaSorting class and JavaSortingTest class with the additional requirements you mentioned:
JavaSorting.java
import java.util.Arrays;
import java.util.Scanner;
public class JavaSorting {
private int[] arr;
public JavaSorting() {
arr = new int[13];
generateRandomArray();
}
private void generateRandomArray() {
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) (Math.random() * 99) + 1;
}
}
public void sortArray() {
Scanner scanner = new Scanner(System.in);
System.out.println("Select a sorting option:");
System.out.println("1. Bubble Sort");
System.out.println("2. Insertion Sort");
System.out.println("3. Shell Sort");
System.out.println("4. Merge Sort");
System.out.println("5. Quick Sort");
int option = scanner.nextInt();
switch (option) {
case 1:
bubbleSort();
break;
case 2:
insertionSort();
break;
case 3:
shellSort();
break;
case 4:
mergeSort();
break;
case 5:
quickSort();
break;
default:
System.out.println("Invalid option!");
}
}
private void bubbleSort() {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(j, j + 1);
System.out.println(Arrays.toString(arr));
}
}
}
}
private void insertionSort() {
int n = arr.length;
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
System.out.println(Arrays.toString(arr));
}
}
private void shellSort() {
int n = arr.length;
for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = temp;
}
System.out.println(Arrays.toString(arr));
}
}
private void mergeSort() {
mergeSortHelper(0, arr.length - 1);
}
private void mergeSortHelper(int left, int right) {
if (left < right) {
int middle = left + (right - left) / 2;
mergeSortHelper(left, middle);
mergeSortHelper(middle + 1, right);
merge(left, middle, right);
System.out.println(Arrays.toString(arr));
}
}
private void merge(int left, int middle, int right) {
int[] tempArr = new int[arr.length];
for (int i = left; i <= right; i++) {
tempArr[i] = arr[i];
}
int i = left;
int j = middle + 1;
int k = left;