Merge sort
Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l…m] and arr[m+1…r] are sorted and merges the two sorted sub-arrays into one.
MergeSort(arr[], l, r)
If r > l
1. Find the middle point to divide the array into two halves:
middle m = (l+r)/2
2. Call mergeSort for first half:
Call mergeSort(arr, l, m)
3. Call mergeSort for second half:
Call mergeSort(arr, m+1, r)
4. Merge the two halves sorted in step 2 and 3:
Call merge(arr, l, m, r)
public class Study {
/**
* main function to sort array using merge sort
*
* @param args
*/
public static void main(String[] args) {
int[] arr = {1, -10, 100, -100, 0, 2, 5, 8, 25};
print(arr);
System.out.println();
mergeAsc(arr);
// if we want descending order we just reverse the ascending order
print(arr);
}
/**
* merge sort arr
*
* @param arr
*/
private static void mergeAsc(int[] arr) {
mergeSort(arr, 0, arr.length - 1);
}
/**
* merge sort arr index from start to end
*
* @param arr
* @param start
* @param end
*/
public static void mergeSort(int[] arr, int start, int end) {
if (start >= end) return; // if start >= end directly return
int mid = (start + end) / 2; // get the mid index
mergeSort(arr, start, mid); // sort the left part
mergeSort(arr, mid + 1, end); // sort the right part
merge(arr, start, mid, end); // merge the two part together
}
/**
* merge two sorted array into one array
*
* @param arr
* @param start
* @param mid
* @param end
*/
public static void merge(int[] arr, int start, int mid, int end) {
// if the first part's largest element is smaller than
// the second part's smallest element directly return
if (arr[mid] <= arr[mid + 1]) return;
// i, j and tempIndex to merge two parts together
int i = start;
int j = mid + 1;
int tempIndex = 0;
// temp array to hold sorted element from start to end in the original array
int[] temp = new int[end - start + 1];
// keep merge two array when any one of the two parts still have some elements
while (i <= mid && j <= end) {
temp[tempIndex++] = arr[i] <= arr[j] ? arr[i++] : arr[j++];
}
while (i <= mid) { // copy left part into temp
temp[tempIndex++] = arr[i++];
}
while (j <= end) { // copy right part into temp
temp[tempIndex++] = arr[j++];
}
int idx = start; // copy element from temp into original array from index start to end
for (int k = 0; k < tempIndex; k++) {
arr[idx++] = temp[k];
}
}
/**
* print the array
*
* @param arr
*/
private static void print(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print("arr[" + i + "] = " + arr[i] + " ");
}
}
}
Question
When we want to make this sort into descending order, how can we do this?
- Sort it into ascending order and then reverse the whole array into descending order
- Modify the code to directly sort array into descending order, I still don’t figure out it, how to modify this code???