mergeSort1(dest, src, low, mid); mergeSort1(dest, src, mid, high);
private static void mergeSort1(byte src[], byte dest[], int low, int high) { int length = high - low; // Insertion sort on smallest arrays if (length < SMALL) { for (int i = low; i < high; i++) for (int j = i; j > low && dest[j - 1] > dest[j]; j--) swap(dest, j, j - 1); return; } // Recursively sort halves of dest into src int mid = (low + high) / 2; mergeSort1(dest, src, low, mid); mergeSort1(dest, src, mid, high); // If list is already sorted, just copy from src to dest. This is an // optimization that results in faster sorts for nearly ordered lists. if (src[mid - 1] <= src[mid]) { System.arraycopy(src, low, dest, low, length); return; } // Merge sorted halves (now in src) into dest for (int i = low, p = low, q = mid; i < high; i++) { if (q >= high || p < mid && src[p] <= src[q]) dest[i] = src[p++]; else dest[i] = src[q++]; } }
private static void mergeSort1(double src[], double dest[], int low, int high) { int length = high - low; // Insertion sort on smallest arrays if (length < SMALL) { for (int i = low; i < high; i++) for (int j = i; j > low && dest[j - 1] > dest[j]; j--) swap(dest, j, j - 1); return; } // Recursively sort halves of dest into src int mid = (low + high) / 2; mergeSort1(dest, src, low, mid); mergeSort1(dest, src, mid, high); // If list is already sorted, just copy from src to dest. This is an // optimization that results in faster sorts for nearly ordered lists. if (src[mid - 1] <= src[mid]) { System.arraycopy(src, low, dest, low, length); return; } // Merge sorted halves (now in src) into dest for (int i = low, p = low, q = mid; i < high; i++) { if (q >= high || p < mid && src[p] <= src[q]) dest[i] = src[p++]; else dest[i] = src[q++]; } }
private static void mergeSort1(long src[], long dest[], int low, int high) { int length = high - low; // Insertion sort on smallest arrays if (length < SMALL) { for (int i = low; i < high; i++) for (int j = i; j > low && dest[j - 1] > dest[j]; j--) swap(dest, j, j - 1); return; } // Recursively sort halves of dest into src int mid = (low + high) / 2; mergeSort1(dest, src, low, mid); mergeSort1(dest, src, mid, high); // If list is already sorted, just copy from src to dest. This is an // optimization that results in faster sorts for nearly ordered lists. if (src[mid - 1] <= src[mid]) { System.arraycopy(src, low, dest, low, length); return; } // Merge sorted halves (now in src) into dest for (int i = low, p = low, q = mid; i < high; i++) { if (q >= high || p < mid && src[p] <= src[q]) dest[i] = src[p++]; else dest[i] = src[q++]; } }
private static void mergeSort1(byte src[], byte dest[], int low, int high) { int length = high - low; // Insertion sort on smallest arrays if (length < SMALL) { for (int i = low; i < high; i++) for (int j = i; j > low && dest[j - 1] > dest[j]; j--) swap(dest, j, j - 1); return; } // Recursively sort halves of dest into src int mid = (low + high) / 2; mergeSort1(dest, src, low, mid); mergeSort1(dest, src, mid, high); // If list is already sorted, just copy from src to dest. This is an // optimization that results in faster sorts for nearly ordered lists. if (src[mid - 1] <= src[mid]) { System.arraycopy(src, low, dest, low, length); return; } // Merge sorted halves (now in src) into dest for (int i = low, p = low, q = mid; i < high; i++) { if (q >= high || p < mid && src[p] <= src[q]) dest[i] = src[p++]; else dest[i] = src[q++]; } }
private static void mergeSort1(short src[], short dest[], int low, int high) { int length = high - low; // Insertion sort on smallest arrays if (length < SMALL) { for (int i = low; i < high; i++) for (int j = i; j > low && dest[j - 1] > dest[j]; j--) swap(dest, j, j - 1); return; } // Recursively sort halves of dest into src int mid = (low + high) / 2; mergeSort1(dest, src, low, mid); mergeSort1(dest, src, mid, high); // If list is already sorted, just copy from src to dest. This is an // optimization that results in faster sorts for nearly ordered lists. if (src[mid - 1] <= src[mid]) { System.arraycopy(src, low, dest, low, length); return; } // Merge sorted halves (now in src) into dest for (int i = low, p = low, q = mid; i < high; i++) { if (q >= high || p < mid && src[p] <= src[q]) dest[i] = src[p++]; else dest[i] = src[q++]; } }
mergeSort1(dest, src, low, mid, c); mergeSort1(dest, src, mid, high, c);
mergeSort1(dest, src, low, mid, c); mergeSort1(dest, src, mid, high, c);
private static void mergeSort1(short src[], short dest[], int low, int high, ShortComparator c) { int length = high - low; // Insertion sort on smallest arrays if (length < SMALL) { for (int i = low; i < high; i++) for (int j = i; j > low && c.compare(dest[j - 1], dest[j]) > 0; j--) swap(dest, j, j - 1); return; } // Recursively sort halves of dest into src int mid = (low + high) / 2; mergeSort1(dest, src, low, mid, c); mergeSort1(dest, src, mid, high, c); // If list is already sorted, just copy from src to dest. This is an // optimization that results in faster sorts for nearly ordered lists. if (c.compare(src[mid - 1], src[mid]) <= 0) { System.arraycopy(src, low, dest, low, length); return; } // Merge sorted halves (now in src) into dest for (int i = low, p = low, q = mid; i < high; i++) { if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0) dest[i] = src[p++]; else dest[i] = src[q++]; } }
mergeSort1(dest, src, low, mid, c); mergeSort1(dest, src, mid, high, c);
mergeSort1(dest, src, low, mid, c); mergeSort1(dest, src, mid, high, c);
/** * Sorts the specified range of the specified array of elements. * * <p>This sort is guaranteed to be <i>stable</i>: equal elements will * not be reordered as a result of the sort.<p> * * The sorting algorithm is a modified mergesort (in which the merge is * omitted if the highest element in the low sublist is less than the * lowest element in the high sublist). This algorithm offers guaranteed * n*log(n) performance, and can approach linear performance on nearly * sorted lists. * * @param a the array to be sorted. * @param fromIndex the index of the first element (inclusive) to be * sorted. * @param toIndex the index of the last element (exclusive) to be sorted. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or * <tt>toIndex > a.length</tt> */ public static void mergeSort(char[] a, int fromIndex, int toIndex) { rangeCheck(a.length, fromIndex, toIndex); char aux[] = (char[]) a.clone(); mergeSort1(aux, a, fromIndex, toIndex); } /**
/** * Sorts the specified range of the specified array of elements. * * <p>This sort is guaranteed to be <i>stable</i>: equal elements will * not be reordered as a result of the sort.<p> * * The sorting algorithm is a modified mergesort (in which the merge is * omitted if the highest element in the low sublist is less than the * lowest element in the high sublist). This algorithm offers guaranteed * n*log(n) performance, and can approach linear performance on nearly * sorted lists. * * @param a the array to be sorted. * @param fromIndex the index of the first element (inclusive) to be * sorted. * @param toIndex the index of the last element (exclusive) to be sorted. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or * <tt>toIndex > a.length</tt> */ public static void mergeSort(short[] a, int fromIndex, int toIndex) { rangeCheck(a.length, fromIndex, toIndex); short aux[] = (short[]) a.clone(); mergeSort1(aux, a, fromIndex, toIndex); } /**
/** * Sorts the specified range of the specified array of elements. * * <p>This sort is guaranteed to be <i>stable</i>: equal elements will * not be reordered as a result of the sort.<p> * * The sorting algorithm is a modified mergesort (in which the merge is * omitted if the highest element in the low sublist is less than the * lowest element in the high sublist). This algorithm offers guaranteed * n*log(n) performance, and can approach linear performance on nearly * sorted lists. * * @param a the array to be sorted. * @param fromIndex the index of the first element (inclusive) to be * sorted. * @param toIndex the index of the last element (exclusive) to be sorted. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or * <tt>toIndex > a.length</tt> */ public static void mergeSort(byte[] a, int fromIndex, int toIndex) { rangeCheck(a.length, fromIndex, toIndex); byte aux[] = (byte[]) a.clone(); mergeSort1(aux, a, fromIndex, toIndex); } /**
/** * Sorts the specified range of the specified array of elements. * * <p>This sort is guaranteed to be <i>stable</i>: equal elements will * not be reordered as a result of the sort.<p> * * The sorting algorithm is a modified mergesort (in which the merge is * omitted if the highest element in the low sublist is less than the * lowest element in the high sublist). This algorithm offers guaranteed * n*log(n) performance, and can approach linear performance on nearly * sorted lists. * * @param a the array to be sorted. * @param fromIndex the index of the first element (inclusive) to be * sorted. * @param toIndex the index of the last element (exclusive) to be sorted. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or * <tt>toIndex > a.length</tt> */ public static void mergeSort(int[] a, int fromIndex, int toIndex) { rangeCheck(a.length, fromIndex, toIndex); int aux[] = (int[]) a.clone(); mergeSort1(aux, a, fromIndex, toIndex); } /**
/** * Sorts the specified range of the specified array of elements. * * <p>This sort is guaranteed to be <i>stable</i>: equal elements will * not be reordered as a result of the sort.<p> * * The sorting algorithm is a modified mergesort (in which the merge is * omitted if the highest element in the low sublist is less than the * lowest element in the high sublist). This algorithm offers guaranteed * n*log(n) performance, and can approach linear performance on nearly * sorted lists. * * @param a the array to be sorted. * @param fromIndex the index of the first element (inclusive) to be * sorted. * @param toIndex the index of the last element (exclusive) to be sorted. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or * <tt>toIndex > a.length</tt> */ public static void mergeSort(long[] a, int fromIndex, int toIndex) { rangeCheck(a.length, fromIndex, toIndex); long aux[] = (long[]) a.clone(); mergeSort1(aux, a, fromIndex, toIndex); } /**
/** * Sorts the specified range of the specified array of elements. * * <p>This sort is guaranteed to be <i>stable</i>: equal elements will * not be reordered as a result of the sort.<p> * * The sorting algorithm is a modified mergesort (in which the merge is * omitted if the highest element in the low sublist is less than the * lowest element in the high sublist). This algorithm offers guaranteed * n*log(n) performance, and can approach linear performance on nearly * sorted lists. * * @param a the array to be sorted. * @param fromIndex the index of the first element (inclusive) to be * sorted. * @param toIndex the index of the last element (exclusive) to be sorted. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or * <tt>toIndex > a.length</tt> */ public static void mergeSort(byte[] a, int fromIndex, int toIndex) { rangeCheck(a.length, fromIndex, toIndex); byte aux[] = (byte[]) a.clone(); mergeSort1(aux, a, fromIndex, toIndex); } /**
/** * Sorts the specified range of the specified array of elements. * * <p>This sort is guaranteed to be <i>stable</i>: equal elements will * not be reordered as a result of the sort.<p> * * The sorting algorithm is a modified mergesort (in which the merge is * omitted if the highest element in the low sublist is less than the * lowest element in the high sublist). This algorithm offers guaranteed * n*log(n) performance, and can approach linear performance on nearly * sorted lists. * * @param a the array to be sorted. * @param fromIndex the index of the first element (inclusive) to be * sorted. * @param toIndex the index of the last element (exclusive) to be sorted. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or * <tt>toIndex > a.length</tt> */ public static void mergeSort(int[] a, int fromIndex, int toIndex) { rangeCheck(a.length, fromIndex, toIndex); int aux[] = (int[]) a.clone(); mergeSort1(aux, a, fromIndex, toIndex); } /**
/** * Sorts the specified range of the specified array of elements. * * <p>This sort is guaranteed to be <i>stable</i>: equal elements will * not be reordered as a result of the sort.<p> * * The sorting algorithm is a modified mergesort (in which the merge is * omitted if the highest element in the low sublist is less than the * lowest element in the high sublist). This algorithm offers guaranteed * n*log(n) performance, and can approach linear performance on nearly * sorted lists. * * @param a the array to be sorted. * @param fromIndex the index of the first element (inclusive) to be * sorted. * @param toIndex the index of the last element (exclusive) to be sorted. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or * <tt>toIndex > a.length</tt> */ public static void mergeSort(long[] a, int fromIndex, int toIndex) { rangeCheck(a.length, fromIndex, toIndex); long aux[] = (long[]) a.clone(); mergeSort1(aux, a, fromIndex, toIndex); } /**
/** * Sorts the specified range of the specified array of elements. * * <p>This sort is guaranteed to be <i>stable</i>: equal elements will * not be reordered as a result of the sort.<p> * * The sorting algorithm is a modified mergesort (in which the merge is * omitted if the highest element in the low sublist is less than the * lowest element in the high sublist). This algorithm offers guaranteed * n*log(n) performance, and can approach linear performance on nearly * sorted lists. * * @param a the array to be sorted. * @param fromIndex the index of the first element (inclusive) to be * sorted. * @param toIndex the index of the last element (exclusive) to be sorted. * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or * <tt>toIndex > a.length</tt> */ public static void mergeSort(short[] a, int fromIndex, int toIndex) { rangeCheck(a.length, fromIndex, toIndex); short aux[] = (short[]) a.clone(); mergeSort1(aux, a, fromIndex, toIndex); } /**