/** * Returns the index of the median of the three indexed chars. */ private static int med3(int x[], int a, int b, int c, IntComparator comp) { int ab = comp.compare(x[a],x[b]); int ac = comp.compare(x[a],x[c]); int bc = comp.compare(x[b],x[c]); return (ab<0 ? (bc<0 ? b : ac<0 ? c : a) : (bc>0 ? b : ac>0 ? c : a)); } /**
/** * Returns the index of the median of the three indexed chars. */ private static int med3(int x[], int a, int b, int c, IntComparator comp) { int ab = comp.compare(x[a],x[b]); int ac = comp.compare(x[a],x[c]); int bc = comp.compare(x[b],x[c]); return (ab<0 ? (bc<0 ? b : ac<0 ? c : a) : (bc>0 ? b : ac>0 ? c : a)); } /**
/** * Returns the index of the median of the three indexed chars. */ private static int med3(int x[], int a, int b, int c, IntComparator comp) { int ab = comp.compare(x[a], x[b]); int ac = comp.compare(x[a], x[c]); int bc = comp.compare(x[b], x[c]); return (ab < 0 ? (bc < 0 ? b : ac < 0 ? c : a) : (bc > 0 ? b : ac > 0 ? c : a)); }
/** * Returns the index of the median of the three indexed chars. */ private static int med3(int a, int b, int c, IntComparator comp) { int ab = comp.compare(a, b); int ac = comp.compare(a, c); int bc = comp.compare(b, c); return (ab < 0 ? (bc < 0 ? b : ac < 0 ? c : a) : (bc > 0 ? b : ac > 0 ? c : a)); }
/** Finds the given key "a" within some generic data using the binary search algorithm. @param a the index of the key to search for. @param from the leftmost search position, inclusive. @param to the rightmost search position, inclusive. @param comp the comparator determining the order of the generic data. Takes as first argument the index <tt>a</tt> within the generic splitters <tt>s</tt>. Takes as second argument the index <tt>b</tt> within the generic data <tt>g</tt>. @return index of the search key, if it is contained in the list; otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The <i>insertion point</i> is defined as the the point at which the value would be inserted into the list: the index of the first element greater than the key, or <tt>list.length</tt>, if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found. */ private static int binarySearchFromTo(int a, int from, int to, IntComparator comp) { while (from <= to) { int mid = (from + to) / 2; int comparison = comp.compare(mid,a); if (comparison < 0) from = mid + 1; else if (comparison > 0) to = mid - 1; else return mid; // key found } return -(from + 1); // key not found. } /**
/** Finds the given key "a" within some generic data using the binary search algorithm. @param a the index of the key to search for. @param from the leftmost search position, inclusive. @param to the rightmost search position, inclusive. @param comp the comparator determining the order of the generic data. Takes as first argument the index <tt>a</tt> within the generic splitters <tt>s</tt>. Takes as second argument the index <tt>b</tt> within the generic data <tt>g</tt>. @return index of the search key, if it is contained in the list; otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The <i>insertion point</i> is defined as the the point at which the value would be inserted into the list: the index of the first element greater than the key, or <tt>list.length</tt>, if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found. */ private static int binarySearchFromTo(int a, int from, int to, IntComparator comp) { while (from <= to) { int mid = (from + to) / 2; int comparison = comp.compare(mid,a); if (comparison < 0) from = mid + 1; else if (comparison > 0) to = mid - 1; else return mid; // key found } return -(from + 1); // key not found. } /**
while (from <= to) { int mid = (from + to) / 2; int comparison = comp.compare(dummy,mid); if (comparison < 0) from = mid + 1; else if (comparison > 0) to = mid - 1;
while (from <= to) { int mid = (from + to) / 2; int comparison = comp.compare(dummy,mid); if (comparison < 0) from = mid + 1; else if (comparison > 0) to = mid - 1;
int half = len / 2; int middle = first + half; if (comp.compare(middle, x)<0) { first = middle + 1; len -= half + 1;
int half = len / 2; int middle = first + half; if (comp.compare(x, middle)<0) { len = half;
int half = len / 2; int middle = first + half; if (comp.compare(x, middle)<0) { len = half;
/** Same as {@link #partition(int[],int,int,int)} except that it <i>generically</i> partitions arbitrary shaped data (for example matrices or multiple arrays) rather than <tt>int[]</tt> arrays. */ private static int genericPartition(int from, int to, int splitter, IntComparator comp, Swapper swapper) { for (int i=from-1; ++i<=to; ) { if (comp.compare(splitter,i) > 0) { // swap x[i] with x[from] swapper.swap(i,from); from++; } } return from-1; } /**
/** Same as {@link #partition(int[],int,int,int)} except that it <i>generically</i> partitions arbitrary shaped data (for example matrices or multiple arrays) rather than <tt>int[]</tt> arrays. */ private static int genericPartition(int from, int to, int splitter, IntComparator comp, Swapper swapper) { for (int i=from-1; ++i<=to; ) { if (comp.compare(splitter,i) > 0) { // swap x[i] with x[from] swapper.swap(i,from); from++; } } return from-1; } /**
private static void mergeSort1(int src[], int dest[], int low, int high, IntComparator 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++]; } }