Lesson 3 Hacks
Hack and Homework for lesson 3.
void merge(String arr[], int l, int m, int r) {
// Find the sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
String[] L = new String[n1];
String[] R = new String[n2];
/* Copy data to temp arrays */
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarray array
int k = l;
while (i < n1 && j < n2) {
if (L[i].compareTo(R[j]) <= 0) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
public class MergeSort {
public static void mergeSort(int[] arr, int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
public static void merge(int[] arr, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int[] leftArr = new int[n1];
int[] rightArr = new int[n2];
for (int i = 0; i < n1; i++) {
leftArr[i] = arr[left + i];
}
for (int j = 0; j < n2; j++) {
rightArr[j] = arr[mid + 1 + j];
}
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftArr[i] <= rightArr[j]) {
arr[k] = leftArr[i];
i++;
} else {
arr[k] = rightArr[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = leftArr[i];
i++;
k++;
}
while (j < n2) {
arr[k] = rightArr[j];
j++;
k++;
}
}
public static void main(String[] args) {
int[] arr = {10, 7, 8, 9, 1, 5};
System.out.println("Before:" + Arrays.toString(arr));
mergeSort(arr, 0, arr.length - 1);
System.out.println("After:" + Arrays.toString(arr));
}
}
MergeSort.main(null);
public class BinarySearchRecursive {
public static int binarySearch(int[] arr, int x, int low, int high) {
if (low > high) {
return -1; // Element not found
}
int mid = low + (high - low) / 2;
if (arr[mid] == x) {
return mid; // Element found at mid index
} else if (arr[mid] > x) {
return binarySearch(arr, x, low, mid - 1); // Search in left half
} else {
return binarySearch(arr, x, mid + 1, high); // Search in right half
}
}
public static void main(String[] args) {
int[] arr = {1, 3, 5, 7, 9, 23, 45, 67};
int x = 45;
int index = binarySearch(arr, x, 0, arr.length - 1);
if (index == -1) {
System.out.println("Element not found in array");
} else {
System.out.println("Element found at index: " + index);
}
}
}
BinarySearchRecursive.main(null);
public class MergeSortAndBinarySearch {
public static void main(String[] args) {
int[] array = {5, 6, 3, 1, 8, 9, 4, 7, 2};
int searchElement = 7;
// Sort the array using merge sort
mergeSort(array, 0, array.length - 1);
// Perform binary search on sorted array to find index of searchElement
int index = binarySearch(array, searchElement, 0, array.length - 1);
if (index == -1) {
System.out.println("Element not found in array");
} else {
System.out.println("Element found at index: " + index);
}
}
public static void mergeSort(int[] arr, int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
public static void merge(int[] arr, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int[] leftArr = new int[n1];
int[] rightArr = new int[n2];
for (int i = 0; i < n1; i++) {
leftArr[i] = arr[left + i];
}
for (int j = 0; j < n2; j++) {
rightArr[j] = arr[mid + 1 + j];
}
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftArr[i] <= rightArr[j]) {
arr[k++] = leftArr[i++];
} else {
arr[k++] = rightArr[j++];
}
}
while (i < n1) {
arr[k++] = leftArr[i++];
}
while (j < n2) {
arr[k++] = rightArr[j++];
}
}
public static int binarySearch(int[] arr, int x, int low, int high) {
if (low > high) {
return -1; // Element not found
}
int mid = low + (high - low) / 2;
if (arr[mid] == x) {
return mid; // Element found at mid index
} else if (arr[mid] > x) {
return binarySearch(arr, x, low, mid - 1); // Search in left half
} else {
return binarySearch(arr, x, mid + 1, high); // Search in right half
}
}
}
MergeSortAndBinarySearch.main(null);