Java Array Sort sortByValueStable(int start, int end, double[] values, int[] indexes)

Here you can find the source of sortByValueStable(int start, int end, double[] values, int[] indexes)

Description

In-place sort of two arrays, only indexes is used for comparison and values of same position are sorted accordingly.

License

Apache License

Parameter

Parameter Description
start start index
end end index
values double array of values to sort
indexes int array of indexes to sort by

Declaration

public static void sortByValueStable(int start, int end, double[] values, int[] indexes) 

Method Source Code


//package com.java2s;
/*/*from   w  ww .  j a v  a2s  . co m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import java.util.Arrays;

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 start index
      * @param end end index
      * @param values double array of values to sort
      * @param indexes int array of indexes to sort by
      */
    public static void sortByValueStable(int start, int end, double[] values, int[] indexes) {

        sortByValue(start, end, values, indexes);

        // Maintain the stability of the index order.
        for (int i = 0; i < values.length - 1; i++) {
            double tmp = values[i];
            //determine run of equal values
            int len = 0;
            while (i + len + 1 < values.length && tmp == values[i + len + 1])
                len++;
            //unstable sort of run indexes (equal value guaranteed)
            if (len > 0) {
                Arrays.sort(indexes, i, i + len + 1);
                i += len; //skip processed run
            }
        }
    }

    public static void sortByValue(int start, int end, double[] values, int[] indexes) {
        double tempVal;
        int tempIx;

        int length = end - start;
        if (length < 7) {
            for (int i = start + 1; i < end; i++) {
                for (int j = i; j > start && values[j - 1] > values[j]; j--) {
                    tempVal = values[j];
                    values[j] = values[j - 1];
                    values[j - 1] = tempVal;
                    tempIx = indexes[j];
                    indexes[j] = indexes[j - 1];
                    indexes[j - 1] = tempIx;
                }
            }
            return;
        }
        int middle = (start + end) / 2;
        if (length > 7) {
            int bottom = start;
            int top = end - 1;
            if (length > 40) {
                length /= 8;
                bottom = med3(values, bottom, bottom + length, bottom + (2 * length));
                middle = med3(values, middle - length, middle, middle + length);
                top = med3(values, top - (2 * length), top - length, top);
            }
            middle = med3(values, bottom, middle, top);
        }
        double partionValue = values[middle];
        int a, b, c, d;
        a = b = start;
        c = d = end - 1;
        while (true) {
            while (b <= c && values[b] <= partionValue) {
                if (values[b] == partionValue) {
                    tempVal = values[a];
                    values[a] = values[b];
                    values[b] = tempVal;
                    tempIx = indexes[a];
                    indexes[a++] = indexes[b];
                    indexes[b] = tempIx;
                }
                b++;
            }
            while (c >= b && values[c] >= partionValue) {
                if (values[c] == partionValue) {
                    tempVal = values[c];
                    values[c] = values[d];
                    values[d] = tempVal;
                    tempIx = indexes[c];
                    indexes[c] = indexes[d];
                    indexes[d--] = tempIx;
                }
                c--;
            }
            if (b > c) {
                break;
            }
            tempVal = values[b];
            values[b] = values[c];
            values[c] = tempVal;
            tempIx = indexes[b];
            indexes[b++] = indexes[c];
            indexes[c--] = tempIx;
        }
        length = a - start < b - a ? a - start : b - a;
        int l = start;
        int h = b - length;
        while (length-- > 0) {
            tempVal = values[l];
            values[l] = values[h];
            values[h] = tempVal;
            tempIx = indexes[l];
            indexes[l++] = indexes[h];
            indexes[h++] = tempIx;
        }
        length = d - c < end - 1 - d ? d - c : end - 1 - d;
        l = b;
        h = end - length;
        while (length-- > 0) {
            tempVal = values[l];
            values[l] = values[h];
            values[h] = tempVal;
            tempIx = indexes[l];
            indexes[l++] = indexes[h];
            indexes[h++] = tempIx;
        }
        if ((length = b - a) > 0) {
            sortByValue(start, start + length, values, indexes);
        }
        if ((length = d - c) > 0) {
            sortByValue(end - length, end, values, indexes);
        }
    }

    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));
    }

    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));
    }
}

Related

  1. sortByIndex(int start, int end, int[] indexes, double[] values)
  2. sortByLength(String[] proposals)
  3. sortByLengthDesc(String[] ss)
  4. sortByString(Object[] array)
  5. sortByValue(int start, int end, double[] values, int[] indexes)
  6. sortCompare(String[] valuesOne, String[] valuesTwo)
  7. sortDesc(long[] keys, int[] values)
  8. sortDescending(T[] a, int fromIndex, int toIndex)
  9. SortDoubleDimensionArray(String StrArr[][])