I need help with this Java program. That\'s I have so far. And I need to fill ou
ID: 3722224 • 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
Name this project SortTimer2 351-03, Spring 2018, Lab 6 a class SortTimer2 with the following Methods: c static void insertionsort (int [1 nums) s method uses the insertion sort to sort an array of integers, named nums, public This into increasing order 2public statie void selectionsort (int [J nums) This into increasing order 8 method uses the selection sort to sort an array of integers, named nums, void mergesort (int[] nums, int left, int right) sorts the subarray nums [left. .right] of the nums array into 3. public static Th for this method: If left mid+1), then if (m S mid) copy the remainder of nums [left..mid] into the result array, otherwise copy the remainder of nums [mid+1. .right] into the result array. Then copy the result array to the subarray nums [left..right]Explanation / Answer
Public void merge(int[] nums, int left, int middle, int right)
{
//find sizes of 2 arrays to be merged
int n1 = middle – left +1;
int n2 = right- middle;
//create temporary arrays
int L[] = new int[n1];
int R[] = new int[n2];
//copy the data into temporary arrays
for(int i=0; i<n1; ++i)
L[i] = nums[left +i];
for(int j=0; j<n2; ++j)
R[j] = nums[middle + 1 + j];
// merge the temporary arrays
int i=0, j=0;
int k=left;
while(i<n1 && j< n2)
{
if(L[i] <= R[j])
{
nums[k] = L[i];
i++;
}
else
{
nums[k] = R[j];
j++;
}
k++;
}
// copy the remainng elements
while(i<n1)
{
nums[k] = L[i];
i++;
k++;
}
while(j<n2)
{
nums[k] = R[j];
j++;
k++;
}
}
public static void mergeSort(int[] nums, int left, int right)
{
if(left<right)
{
int middle = (left+right)/2;
mergeSort(nums, left, middle);
mergeSort(nums, middle+1, right);
//merge the sorted halves
merge(nums, left, middle, right);
}
}
public static void mergeSortPrint(int[] nums, int left, int right)
{
if(left<right)
{
int middle = (left+right)/2;
mergeSort(nums, left, middle);
mergeSort(nums, middle+1, right);
//merge the sorted halves
merge(nums, left, middle, right);
}
System.out.println(''nums = '' + Arrays.toString(nums));
}
public staic 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++;
//swap nums[i] and nums[j]
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
public static void quickSort(int[] nums, int left, int right)
{
if(left<right)
{
int pi = partition(nums, left, right);
System.out.println(''nums = '' + Arrays.toString(nums));
sort(nums, left, pi-1);
sort(nums, pi+1, right);
}
}
public static void quickSortPrint(int[] nums, int left, int right)
{
if(left<right)
{
int pi = partition(nums, left, right);
sort(nums, left, pi-1);
sort(nums, pi+1, right);
}
}