JAVA PROBLEM You must ask the user to select which sorting routine he/she wants
ID: 3696297 • Letter: J
Question
JAVA PROBLEM You must ask the user to select which sorting routine he/she wants to use out of those routines implemented in the book (Insertion Sort, Shell Sort, MergeSort, or QuickSort). Based on the user input, you need to call the corresponding routine. You are required to write one main program that will read in any number of integers and store it in an array of ints (not an ArrayList). What you need to do first is to ask the user what sorting routine he/she would like to use. Then you need to ask the user how many integers to be entered as input, and then ask the user to input that many integers. Your program will store these integers into an array of integers first. Then your program is supposed to remove the duplicates out of this array by first sorting the array (using the user-specified sorting routine), and then removing any duplicates. Your program must not copy these elements into another array to remove the duplicates, the duplicates should be removed in place which means in the same array. Also, you must sort the array first before you start duplicate removal. Once your array (the same array you started with) has only unique numbers, your program should print out the array followed by a list of all the numbers that were duplicated. The numbers that were duplicated can be stored into another array. -------- The following would be a sample scenario of running your program: Make your choice of a sorting routine first. Then continue as follows: Enter the number of integers: 10 Enter the 10 integers: 7 9 8 1 9 24 23 24 9 8 The resulting array is: 1 7 8 9 23 24 The numbers 8, 9, 24 were duplicated in the input. --------
Explanation / Answer
import java.io.ObjectInputStream.GetField;
import java.util.Scanner;
public class AllSorting {
int numbers[] = null;
int n = 0;
Scanner scan = new Scanner(System.in);
void getNumbers() {
System.out.println(" Enter n value :");
n = Integer.parseInt(scan.nextLine());
numbers = new int[n];
System.out.println("Enter the Numbers :");
for(int i=0; i<n; i++) {
numbers[i] = Integer.parseInt(scan.nextLine());
}
}
void SelectionSort() {
System.out.println(" Before Sorting :");
for(int i=0; i<n; i++) {
System.out.print(numbers[i] + " ");
}
/* Selection Sort Code Start */
for(int i=0; i<n; i++) {
int index_of_min = i;
for(int j=i; j<n; j++) {
if(numbers[index_of_min] > numbers[j]) {
index_of_min = j;
}
}
int temp = numbers[i];
numbers[i] = numbers[index_of_min];
numbers[index_of_min] = temp;
}
/* Selection Sort Code End */
System.out.println(" After Sorting");
System.out.println(" Ascending Order :");
for(int i=0; i<n; i++) {
System.out.print(" " + numbers[i]);
}
System.out.println(" Descending Order :");
for(int i=n-1; i>=0; i--) {
System.out.print(" " + numbers[i]);
}
}
void ShellSort() {
System.out.println(" Before Sorting :");
for(int i=0; i<n; i++) {
System.out.print(numbers[i] + " ");
}
/* Shell Sort Code Start */
int inner, outer, temp;
int h = 1;
while (h > 0) {
for (outer = h; outer < n; outer++) {
temp = numbers[outer];
inner = outer;
while (inner > h - 1 && numbers[inner - h] >= temp) {
numbers[inner] = numbers[inner - h];
inner -= h;
}
numbers[inner] = temp;
}
h = (h - 1) / 3; // decrease h
}
/* Shell Sort Code End */
System.out.println(" After Sorting");
System.out.println(" Ascending Order :");
for(int i=0; i<n; i++) {
System.out.print(" " + numbers[i]);
}
System.out.println(" Descending Order :");
for(int i=n-1; i>=0; i--) {
System.out.print(" " + numbers[i]);
}
}
private static void MergeSort(int[] num, int i, int j) {
int low = i;
int high = j;
if (low >= high) {
return;
}
int middle = (low + high) / 2;
MergeSort(num, low, middle);
MergeSort(num, middle + 1, high);
int end_low = middle;
int start_high = middle + 1;
while ((low <= end_low) && (start_high <= high)) {
if (num[low] < num[start_high]) {
low++;
}
else {
int Temp = num[start_high];
for (int k = start_high- 1; k >= low; k--) {
num[k+1] = num[k];
}
num[low] = Temp;
low ++;
end_low ++;
start_high ++;
}
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String choice = null;
choice=sc.next();
AllSorting as=new AllSorting();
switch(choice)
{
case "Insertion" :
as.getNumbers();
as.SelectionSort();
break;
case "Shell" :
as.getNumbers();
as.ShellSort();
break;
case "merge" :
as.getNumbers();
break;
default :
System.out.println("Invalid choice");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.