/* * Finds the place in the given range of specified sorted array, where the * element should be inserted for getting sorted array. Uses exponential * search algorithm. * * @param arr - * the array with already sorted range * * @param val - * object to be inserted * * @param l - * the start index * * @param r - * the end index * * @param bnd - * possible values 0,-1. "-1" - val is located at index more then * elements equals to val. "0" - val is located at index less * then elements equals to val. * */ private static int find(Object[] arr, java.lang.Comparable <Object> val, int bnd, int l, int r) { int m = l; int d = 1; while (m <= r) { if (val.compareTo(arr[m]) > bnd) { l = m + 1; } else { r = m - 1; break; } m += d; d <<= 1; } while (l <= r) { m = java.dotnet.lang.Operator.shiftRightUnsignet((l + r), 1); if (val.compareTo(arr[m]) > bnd) { l = m + 1; } else { r = m - 1; } } return(l - 1); }
public int compare(T o1, T o2) { java.lang.Comparable <T> c2 = (java.lang.Comparable <T>)o2; return(c2.compareTo(o1)); }
/* * Performs a sort on the section of the array between the given indices * using a mergesort with exponential search algorithm (in which the merge * is performed by exponential search). n*log(n) performance is guaranteed * and in the average case it will be faster then any mergesort in which the * merge is performed by linear search. * * @param in - * the array for sorting. * @param out - * the result, sorted array. * @param start * the start index * @param end * the end index + 1 */ private static void mergeSort(Object[] inJ, Object[] outJ, int start, int end) { int len = end - start; // use insertion sort for small arrays if (len <= SIMPLE_LENGTH) { for (int i = start + 1; i < end; i++) { java.lang.Comparable <Object> current = (java.lang.Comparable <Object>)outJ[i]; Object prev = outJ[i - 1]; if (current.compareTo(prev) < 0) { int j = i; do { outJ[j--] = prev; } while (j > start && current.compareTo(prev = outJ[j - 1]) < 0); outJ[j] = current; } } return; } int med = java.dotnet.lang.Operator.shiftRightUnsignet((end + start), 1); mergeSort(outJ, inJ, start, med); mergeSort(outJ, inJ, med, end); // merging // if arrays are already sorted - no merge if (((java.lang.Comparable <Object>)inJ[med - 1]).compareTo(inJ[med]) <= 0) { java.lang.SystemJ.arraycopy(inJ, start, outJ, start, len); return; } int r = med, i2 = start; // use merging with exponential search do { java.lang.Comparable <Object> fromVal = (java.lang.Comparable <Object>)inJ[start]; java.lang.Comparable <Object> rVal = (java.lang.Comparable <Object>)inJ[r]; if (fromVal.compareTo(rVal) <= 0) { int l_1 = find(inJ, rVal, -1, start + 1, med - 1); int toCopy = l_1 - start + 1; java.lang.SystemJ.arraycopy(inJ, start, outJ, i2, toCopy); i2 += toCopy; outJ[i2++] = rVal; r++; start = l_1 + 1; } else { int r_1 = find(inJ, fromVal, 0, r + 1, end - 1); int toCopy = r_1 - r + 1; java.lang.SystemJ.arraycopy(inJ, r, outJ, i2, toCopy); i2 += toCopy; outJ[i2++] = fromVal; start++; r = r_1 + 1; } } while ((end - r) > 0 && (med - start) > 0); // copy rest of array if ((end - r) <= 0) { java.lang.SystemJ.arraycopy(inJ, start, outJ, i2, med - start); } else { java.lang.SystemJ.arraycopy(inJ, r, outJ, i2, end - r); } }