/**
* @param list
*/ public static int[] mergeSort(int[] list) {
/**
* Boundary condition for recursive method.
* An array with ZERO or ONE element is
* already sorted.
*/ if (list == null || list.length <= 1) { return list;
}
/**
* 1. Divide the list into two sub list.
* 2. Sort each subset.
* 3. Merge the two subsets.
*/ int[] leftSubList = new int[list.length/2];
System.arraycopy(list, 0, leftSubList, 0, leftSubList.length);
int[] rightSubList = new int[list.length-leftSubList.length];
System.arraycopy(list, leftSubList.length, rightSubList, 0, rightSubList.length);
/**
* Sorts and returns the time taken in milliseconds.
*
* @param list
* @return
*/ public static double mergeSortTimeTaken(int[] list) { long startTime = System.nanoTime();
mergeSort(list); long timeTaken = System.nanoTime() - startTime; return timeTaken * Math.pow(10, -6);
}
}