Question: This is my code : public class SortModule { private static int[] getFirstHalf(int[] data) { //TODO : update to get first half of array int[]

This is my code :

public class SortModule { private static int[] getFirstHalf(int[] data) { //TODO : update to get first half of array int[] firstHalf = new int[data.length / 2]; for (int i = 0; i < firstHalf.length; i++) { firstHalf[i] = data[i]; } return firstHalf; } private static int[] getSecondHalf(int[] data) { //TODO : update to get second half of array int[] secondHalf = new int[data.length - data.length / 2]; for (int i = 0; i < secondHalf.length; i++) { secondHalf[i] = data[data.length / 2 + i]; } return secondHalf; } private static void merge(int[] data, int[] left, int[] right) { //TODO : update to merge arrays int leftIndex = 0; int rightIndex =0; int dataIndex =0; while (leftIndex < left.length && rightIndex < right.length) { if (left[leftIndex] <= right[rightIndex]) { data[dataIndex] = left[leftIndex]; leftIndex++; } else { data[dataIndex] = right[rightIndex]; rightIndex++; } dataIndex++; } while (leftIndex < left.length) { data[dataIndex] = left[leftIndex]; leftIndex++; dataIndex++; } while (rightIndex < right.length) { data[dataIndex] = right[rightIndex]; rightIndex++; dataIndex++; } } public static void mergeSort(int[] data) { //TODO : update with algorithm if (data.length <= 1) { return; } int[] left = getFirstHalf(data); int[] right = getSecondHalf(data); mergeSort(left); mergeSort(right); merge(data, left, right); } private static int partition(int[] data, int low, int high) { //TODO: update with partition algorithm int pivot = data[low]; int i = low - 1; int j = high + 1; while (true) { do { i++; } while (data[i] < pivot); do { j--; } while (data[j] > pivot); if (i >= j) { return j; } int temp = data[i]; data[i] = data[j]; data[j] = temp; } } public static void quickSort(int[] data) { //TODO : update to call helper method quickSort(data, 0, data.length - 1); } private static void quickSort(int[] data, int min, int max) { //TODO: update to partition list if (min >= max) { return; } int pivotIndex = partition(data, min, max); quickSort(data, min, pivotIndex - 1); quickSort(data, pivotIndex + 1, max); } public static void selectionSort(int[] data) { selectionSort(data, 0); } private static void selectionSort(int[] data, int start) { int minIndex; if(start < data.length - 1){ minIndex = start; for (int i = start + 1; i < data.length; i++) if(data[i] < data[minIndex]) minIndex = i; swap(data, start, minIndex); selectionSort(data, start + 1); } } public static void swap(int[] data, int a, int b) { int temp = data[a]; data[a] = data[b]; data[b] = temp; } }

I want to fix the code below to make it right with my code:

public class SearchModule { public static int binarySearch(int[] data, int target) { return binarySearch(data, target, 0, data.length - 1); } private static int binarySearch(int[] data, int target, int min, int max) { int mid; if(min > max ){ return - 1; }else{ mid = (min + max) / 2; if(data[mid] < target) return binarySearch(data, target, mid + 1, max); else if(data[mid] > target) return binarySearch(data, target, min, mid - 1); else return mid; } } }

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 Databases Questions!