55.3k views
3 votes
Write a Java Sorting Application with two classes, JavaSort and JavaSortTest. Your JavaSort Class, as a minimum must contain sorting methods for BubbleSort, InsertionSort, ShellSort, MergeSort, and QuickSort. Your application must generate: - an array of thirteen random integers from 1-99, - then prompt the user to select a sorting option (Bubble Sort, Insertion Sort, Shell Sort, Merge Sort, or Quick Sort) Your application must: - Account for duplications - Show each completed pass of the sort on a new line with arrays surrounded by square brackets until sorted

*I need help with the last part which is Account for duplications - Show each completed pass of the sort on a new line with arrays surrounded by square brackets until sorted*

User Mojbro
by
7.4k points

1 Answer

0 votes

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;

User Renald
by
7.3k points