Merge Sort Hack #1

Use the integer mergesort that we created and adapt it to sort an array of Java String objects. We recommend using the compareTo() method of the String class for this.

class Main {
    // Merges two subarrays of arr[].
    // First subarray is arr[l..m]
    // Second subarray is arr[m+1..r]
    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]; // copy element from L to arr
                i++; // move to the next element in L
            }
            else {
                arr[k] = R[j]; // copy element from R to arr
                j++; // move to the next element in R
            }
            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++;
        }
    }

    // Main function that sorts arr[l..r] using
    // merge()
    void sort(String[] arr, int l, int r)
    {
        if (l < r) {
            int m = l + (r - l) / 2;

            sort(arr, l, m);
            sort(arr, m + 1, r);

            merge(arr, l, m, r);
        }
    }

    /* A utility function to print array of size n */
    static void printArray(String[] arr)
    {
        int n = arr.length;
        for (int i = 0; i < n; ++i)
            System.out.print(arr[i] + " ");
        System.out.println();
    }

    // Driver code
    public static void main(String args[])
    {
        String[] arr = { "bobby", "amanda", "peter", "steven", "katelyn", "grant" };

        System.out.println("Given Array");
        printArray(arr);

        Main ob = new Main();
        ob.sort(arr, 0, arr.length - 1);

        System.out.println("\nSorted array");
        printArray(arr);
    }
}

Main.main(null)
Given Array
bobby amanda peter steven katelyn grant 

Sorted array
amanda bobby grant katelyn peter steven 

Binary Search Hack #1

Given an int array[] = {1, 3, 5, 7, 9, 23, 45, 67}, search the number 45 and give it's index with Binary search, BUT do this using recursion. Make sure to include informative comments to explain the code as you write the algorithm.

public class BinarySearchRecursive {
    // Define the recursive binary search method
    public static int binarySearch(int arr[], int low, int high, int target) {
        // Check if the base case has been reached
        if (high >= low) {
            // Calculate the middle index of the array
            int mid = low + (high - low) / 2;
            // If the target is present at the middle
            if (arr[mid] == target) {
                return mid;
            }
            // If the target is smaller than the middle element,
            // then it can only be present in the left subarray
            if (arr[mid] > target) {
                return binarySearch(arr, low, mid - 1, target);
            }
            // Else the target can only be present in the right subarray
            return binarySearch(arr, mid + 1, high, target);
        }
        // If the target is not present in the array
        return -1;
    }
    public static void main(String args[]) {
        int arr[] = {1, 3, 5, 7, 9, 23, 45, 67};
        int n = arr.length;
        int target = 45;
        // Perform binary search using recursion
        int result = binarySearch(arr, 0, n - 1, target);
        // Print the result
        if (result == -1) {
            System.out.println("Element not present in the array");
        } else {
            System.out.println("Element found at index " + result);
        }
    }
}
BinarySearchRecursive.main(null)
Element found at index 6