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

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

Description

sort By Value

License

Apache License

Declaration

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

Method Source Code

//package com.java2s;
/*/*from   w  ww.  j av a  2 s . c om*/
 * 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.
 */

public class Main {
    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. sortByIndex(int start, int end, int[] indexes, double[] values)
  3. sortByLength(String[] proposals)
  4. sortByLengthDesc(String[] ss)
  5. sortByString(Object[] array)
  6. sortByValueStable(int start, int end, double[] values, int[] indexes)
  7. sortCompare(String[] valuesOne, String[] valuesTwo)
  8. sortDesc(long[] keys, int[] values)
  9. sortDescending(T[] a, int fromIndex, int toIndex)