I need help with this Java program. That\'s I have so far. And I need to fill ou
ID: 3722123 • Letter: I
Question
I need help with this Java program. That's I have so far. And I need to fill out these methods:
public static void mergeSort(int[] nums, int left, int right)
public static void mergeSortPrint(int[] nums, int left, int right)
public static void quickSort(int[] nums, int left, int right )
public static int partition(int[] nums, int left, int right)
public static void quickSortPrint(int[] nums, int left, int right )
Thank you
1351-03, Spring 2018, Lab 6 Name this proiect SortTimer2 Create a Class SortTimer2 with the following Methods: ic atatic void insertionsort (int 1 nums) hethod uses the insertion sort to sort an array of integers, named nums nto increasing order c static void selectionsort (int [] nums) s method uses the selection sort to sort an array of integers, named nums 2 public Thi into increasing order c static void mergesort (int [ nums, int left, int right) is method sorts the subarray nums [left..right] of the nums array into 3. public in cr easing order using the merge sort algorithm. Use the following pseudocode for this method: If leftExplanation / Answer
import java.util.Arrays;
class Sortings {
public static int partition(int nums[], int left, int right) {
int pivot = nums[right];
int i = (left - 1);
for (int j = left; j < right; j++) {
if (nums[j] <= pivot) {
i++;
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
int temp = nums[i + 1];
nums[i + 1] = nums[right];
nums[right] = temp;
return i + 1;
}
public static void quickSort(int nums[], int left, int right) {
if (left < right) {
int p = partition(nums, left, right);
quickSort(nums, left, p - 1);
quickSort(nums, p + 1, right);
}
}
public static void quickSortPrint(int[] nums, int left, int right) {
System.out.println("nums = " + Arrays.toString(nums));
}
public static void mergeSort(int nums[], int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(nums, left, mid);
mergeSort(nums, mid + 1, right);
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;
/* create temp numsays */
int l_result[] = new int[n1];
int r_result[] = new int[n2];
for (i = 0; i < n1; i++)
l_result[i] = nums[left + i];
for (j = 0; j < n2; j++)
r_result[j] = nums[mid + 1 + j];
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2) {
if (l_result[i] <= r_result[j]) {
nums[k] = l_result[i];
i++;
} else {
nums[k] = r_result[j];
j++;
}
k++;
}
while (i < n1) {
nums[k] = l_result[i];
i++;
k++;
}
while (j < n2) {
nums[k] = r_result[j];
j++;
k++;
}
}
}
public static void mergeSortPrint(int[] nums, int left, int right) {
System.out.println("nums = " + Arrays.toString(nums));
}
public static void main(String args[]) {
int[] numsMerge = { 5, 3, 6, 4, 2, 0, 1 };
mergeSort(numsMerge, 0, numsMerge.length - 1);
mergeSortPrint(numsMerge, 0, numsMerge.length - 1);
quickSort(numsMerge, 0, numsMerge.length - 1);
quickSortPrint(numsMerge, 0, numsMerge.length - 1);
}
}