Here you can find the source of sortByIndex(int start, int end, int[] indexes, double[] values)
Parameter | Description |
---|---|
start | a parameter |
end | a parameter |
indexes | a parameter |
values | a parameter |
public static void sortByIndex(int start, int end, int[] indexes, double[] values)
//package com.java2s; /**//from ww w.j a v a 2s . c om * (C) Copyright IBM Corp. 2010, 2015 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ?*/ public class Main { /** * In-place sort of two arrays, only indexes is used for comparison and values * of same position are sorted accordingly. * * @param start * @param end * @param indexes * @param values */ public static void sortByIndex(int start, int end, int[] indexes, double[] values) { int tempIx; double tempVal; int length = end - start; if (length < 7) { for (int i = start + 1; i < end; i++) { for (int j = i; j > start && indexes[j - 1] > indexes[j]; j--) { tempIx = indexes[j]; indexes[j] = indexes[j - 1]; indexes[j - 1] = tempIx; tempVal = values[j]; values[j] = values[j - 1]; values[j - 1] = tempVal; } } return; } int middle = (start + end) / 2; if (length > 7) { int bottom = start; int top = end - 1; if (length > 40) { length /= 8; bottom = med3(indexes, bottom, bottom + length, bottom + (2 * length)); middle = med3(indexes, middle - length, middle, middle + length); top = med3(indexes, top - (2 * length), top - length, top); } middle = med3(indexes, bottom, middle, top); } int partionValue = indexes[middle]; int a, b, c, d; a = b = start; c = d = end - 1; while (true) { while (b <= c && indexes[b] <= partionValue) { if (indexes[b] == partionValue) { tempIx = indexes[a]; indexes[a] = indexes[b]; indexes[b] = tempIx; tempVal = values[a]; values[a++] = values[b]; values[b] = tempVal; } b++; } while (c >= b && indexes[c] >= partionValue) { if (indexes[c] == partionValue) { tempIx = indexes[c]; indexes[c] = indexes[d]; indexes[d] = tempIx; tempVal = values[c]; values[c] = values[d]; values[d--] = tempVal; } c--; } if (b > c) { break; } tempIx = indexes[b]; indexes[b] = indexes[c]; indexes[c] = tempIx; tempVal = values[b]; values[b++] = values[c]; values[c--] = tempVal; } length = a - start < b - a ? a - start : b - a; int l = start; int h = b - length; while (length-- > 0) { tempIx = indexes[l]; indexes[l] = indexes[h]; indexes[h] = tempIx; tempVal = values[l]; values[l++] = values[h]; values[h++] = tempVal; } length = d - c < end - 1 - d ? d - c : end - 1 - d; l = b; h = end - length; while (length-- > 0) { tempIx = indexes[l]; indexes[l] = indexes[h]; indexes[h] = tempIx; tempVal = values[l]; values[l++] = values[h]; values[h++] = tempVal; } if ((length = b - a) > 0) { sortByIndex(start, start + length, indexes, values); } if ((length = d - c) > 0) { sortByIndex(end - length, end, indexes, values); } } /** * * @param array * @param a * @param b * @param c * @return */ private static int med3(int[] array, int a, int b, int c) { int x = array[a], y = array[b], z = array[c]; return x < y ? (y < z ? b : (x < z ? c : a)) : (y > z ? b : (x > z ? c : a)); } /** * * @param array * @param a * @param b * @param c * @return */ private static int med3(double[] array, int a, int b, int c) { double x = array[a], y = array[b], z = array[c]; return x < y ? (y < z ? b : (x < z ? c : a)) : (y > z ? b : (x > z ? c : a)); } }