Question: rewrite this code with cutoff value public static void main ( String [ ] args ) { int choice; int number; int [ ] array;

rewrite this code with cutoff value public static void main(String[] args){
int choice;
int number;
int[] array;
Scanner input = new Scanner(System.in);
System.out.println("Choose a sorting routine:");
System.out.println("1. Insertion Sort");
System.out.println("2. Shell Sort");
System.out.println("3. Merge Sort");
System.out.println("4. Quick Sort");
choice = input.nextInt();
System.out.print("Enter the number of integers: ");
number = input.nextInt();
array = new int[number];
System.out.println("Enter the "+ number +" integers:");
for (int i =0; i < number; i++){
array[i]= input.nextInt();
}
switch (choice){
case 1:
insertion_sort(array);
break;
case 2:
shell_sort(array);
break;
case 3:
array = merge_sort(array);
break;
case 4:
quick_sort(array,0, array.length -1);
break;
default:
System.out.println("Invalid choice");
return;
}
int count;
count = remove_duplicate(array);
System.out.println("The resulting array is:");
for (int i =0; i < count; i++){
System.out.print(array[i]+"");
}
}
public static void insertion_sort(int[] array){
int key;
int j;
for (int i =1; i < array.length; i++){
key = array[i];
j = i -1;
while (j >=0 && array[j]> key){
array[j +1]= array[j];
j--;
}
array[j +1]= key;
}
}
public static void shell_sort(int[] array){
int number;
int j;
int temporary;
number = array.length;
for (int gap = number /2; gap >0; gap /=2){
for (int i = gap; i < number; i++){
temporary = array[i];
for (j = i; j >= gap && array[j - gap]> temporary; j -= gap){
array[j]= array[j - gap];
}
array[j]= temporary;
}
}
}
public static int[] merge_sort(int[] array){
int middle;
int[] left;
int[] right;
if (array.length <=1) return array;
middle = array.length /2;
left = new int[middle];
right = new int[array.length - middle];
System.arraycopy(array,0, left, 0, middle);
System.arraycopy(array, middle, right, 0, array.length - middle);
left = merge_sort(left);
right = merge_sort(right);
return merge(left, right);
}
public static int[] merge(int[] left, int[] right){
int[] result;
int i;
int j;
int k;
result = new int[left.length + right.length];
i =0;
j =0;
k =0;
while (i < left.length && j < right.length){
if (left[i]<= right[j]){
result[k++]= left[i++];
} else {
result[k++]= right[j++];
}
}
while (i < left.length){
result[k++]= left[i++];
}
while (j < right.length){
result[k++]= right[j++];
}
return result;
}
public static void quick_sort(int[] array, int low, int high){
int pivot_index;
if (low < high){
pivot_index = partition(array, low, high);
quick_sort(array, low, pivot_index -1);
quick_sort(array, pivot_index +1, high);
}
}
public static int partition(int[] array, int low, int high){
int pivot;
int i;
pivot = array[high];
i = low -1;
for (int j = low; j < high; j++){
if (array[j]<= pivot){
i++;
swap(array, i, j);
}
}
swap(array, i +1, high);
return i +1;
}
public static void swap(int[] array, int i, int j){
int temporary;
temporary = array[i];
array[i]= array[j];
array[j]= temporary;
}
public static int remove_duplicate(int[] array){
int index;
if (array.length ==0) return 0;
index =0;
for (int i =1; i < array.length; i++){
if (array[i]!= array[index]){
index++;
array[index]= array[i];
}
}
return index +1;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!