Java Array Sort sortLeftRightAndCenter(double[] array, int[] index, int l, int r)

Here you can find the source of sortLeftRightAndCenter(double[] array, int[] index, int l, int r)

Description

Sorts left, right, and center elements only, returns resulting center as pivot.

License

Open Source License

Declaration

private static int sortLeftRightAndCenter(double[] array, int[] index, int l, int r) 

Method Source Code

//package com.java2s;
/*/*  w  w  w .j  a  v a2 s  .com*/
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Sorts left, right, and center elements only, returns resulting center as pivot.
     */
    private static int sortLeftRightAndCenter(double[] array, int[] index, int l, int r) {

        int c = (l + r) / 2;
        conditionalSwap(array, index, l, c);
        conditionalSwap(array, index, l, r);
        conditionalSwap(array, index, c, r);
        return c;
    }

    /**
     * Conditional swap for quick sort.
     */
    private static void conditionalSwap(double[] array, int[] index, int left, int right) {

        if (array[index[left]] > array[index[right]]) {
            int help = index[left];
            index[left] = index[right];
            index[right] = help;
        }
    }
}

Related

  1. sortIndices(float[] main)
  2. sortInPlace(final double[] v, final int i, final int j)
  3. sortInPlace(int[] array)
  4. sortInterval(byte[] x, int start, int end)
  5. sortKeyValuePairs(long[] keys, double[] values, int startInd, int endInd)
  6. sortMatrixRows(A[] matrix, Comparator comparator)
  7. sortMinMaxToMax(double[] min, double[] max)
  8. sortMinToMax(double[] v)
  9. sortNames(String[] names)