package cookie;public class MergeSort { void mergeSort(int[] a, int[] temp, int left, int right) { if (left < right) { int center = (left + right) / 2; mergeSort(a, temp, left, center); mergeSort(a, temp, center + 1, right); merge(a, temp, left, center + 1, right); } } void merge(int[] a, int[] tempArray, int leftStart, int rightStart, int rightEnd) { int leftEnd = rightStart - 1; int tempPos = leftStart; int numElements = rightEnd - leftStart + 1; while (leftStart <= leftEnd && rightStart <= rightEnd) { if (a[leftStart] <= a[rightStart]) { tempArray[tempPos++] = a[leftStart++]; } else { tempArray[tempPos++] = a[rightStart++]; } while (leftStart <= leftEnd) { tempArray[tempPos++] = a[leftStart++]; } while (rightStart <= rightEnd) { tempArray[tempPos++] = a[rightStart++]; } for (int i = 0; i < numElements; i++, rightEnd--) { a[rightEnd] = tempArray[rightEnd]; } } }}