Problem. Write a Java Sorting Application with two classes, JavaSort and JavaSor
ID: 670301 • Letter: P
Question
Problem. 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 sortedExplanation / Answer
//Implemented bubble and insertion sorts. and print method to print in each step. If you provide the previous code I can rectify the code;
import java.util.Random;
import java.util.Scanner;
public class JavaSortTest {
public static void main(String []args){
//generate 13 random numbers in range of 1-99
int[] input = new int[13];
Random randomGenerator = new Random();
//for(int i = 0; i < 13; i++) {
// int rand= randomGenerator.nextInt(100) ;
// input[i] = rand;
//}
//which elementates duplicates
for(int i = 0; i < 13; i++) {
int rand= randomGenerator.nextInt(100) ;
boolean duplicate = false;
for(int j = 0; j < i; j++) {
if(rand == input[j]) {
duplicate =true;
break;
}
}
if(duplicate)
i--;
else
input[i] = rand;
}
Scanner sc = new Scanner(System.in);
System.out.println("Sortiong options : 1. Bubble sort 2. Insertion sort" +
" 3. Shell sort 4. Merge sort");
System.out.println("Enter sorting option");
int option = sc.nextInt();
JavaSort sort = new JavaSort();
//implemented bubble sort & insertion sort only,
if(option == 1) {
sort.bubbleSort(input);
} else if(option == 2) {
sort.insertionSort(input);
} else if(option == 3) {
//implement the shell sort
sort.shellSort(input);
}else if(option == 4) {
//implement the merge sort
sort.mergeSort(input);
}
}
}
class JavaSort {
public int[] bubbleSort(int[] array) {
int n = array.length;
System.out.println("-------##-----------------Bubble sort----------------------");
System.out.println("Initial array : ");
print(array, 0);
int k;
int step = 1;
for (int m = n; m >= 0; m--) {
for (int i = 0; i < n - 1; i++) {
k = i + 1;
if (array[i] > array[k]) {
swap(i, k, array);
}
}
print(array, step++);
}
return array;
}
private void swap(int i, int j, int[] array) {
int temp;
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
private void print(int[] input, int step) {
System.out.print("Step-"+step+ " [");
for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + ", ");
}
System.out.println("] ");
}
public int[] insertionSort(int[] array) {
System.out.println("-------##-----------------Insertion sort----------------------");
System.out.println("Initial array : ");
print(array, 0);
// insertion sort starts from second element
int step = 1;
for (int i = 1; i < array.length; i++) {
int numberToInsert = array[i];
int compareIndex = i;
while (compareIndex > 0 && array[compareIndex - 1] > numberToInsert) {
array[compareIndex] = array[compareIndex - 1]; // shifting element
compareIndex--; // moving backwards, towards index 0
}
// compareIndex now denotes proper place for number to be sorted
array[compareIndex] = numberToInsert;
print(array, step++);
}
return array;
}
public int[] shellSort(int[] array) {
return null;
}
public int[] mergeSort(int[] array) {
return null;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.