Merge Sort Hack 1
Here is my merge sort hack 1.
// create a class for sorting an array of strings using merge sort
class StringArray {
// method to merge 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) { // use compareTo for strings
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++;
}
}
// main function that sorts arr[l..r] using merge()
void sort(String[] arr, int l, int r) {
if (l < r) {
// find the middle point
int m = l + (r - l) / 2;
// sort first and second halves
sort(arr, l, m);
sort(arr, m + 1, r);
// merge the sorted halves
merge(arr, l, m, r);
}
}
// 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();
}
// tester method
public static void main(String args[]) {
// create an array of strings to be sorted
String[] arr = { "f", "c", "e", "d", "b", "a" };
// print the original array
System.out.println("Given Array");
printArray(arr);
// create an instance of StringArr class and sort the array
StringArr ob = new StringArr();
ob.sort(arr, 0, arr.length - 1);
// print the sorted array
System.out.println();
System.out.println("Sorted array");
printArray(arr);
}
}
// call the main method of StringArr class to execute the program
StringArr.main(null);