Java Array Sort sort( final Item[] values, final Item[] auxiliary, final int first, final int last)

Here you can find the source of sort( final Item[] values, final Item[] auxiliary, final int first, final int last)

Description

sort

License

Open Source License

Declaration

private static <Item extends Comparable<? super Item>> int sort(
            final Item[] values, final Item[] auxiliary, final int first,
            final int last) 

Method Source Code

//package com.java2s;
/*/*from  w  w w . j  a v  a  2  s.  c om*/
 * Copyright 2015, Manuel Menezes de Sequeira.
 * 
 * Copyright 2015, Robert Sedgewick and Kevin Wayne.
 * 
 * This file is in part a derivative work of the code which accompanies the
 * textbook
 * 
 * Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne, Addison-Wesley
 * Professional, 2011, ISBN 0-321-57351-X. http://algs4.cs.princeton.edu
 * 
 * Part of the code derives also from:
 * http://www.geeksforgeeks.org/counting-inversions/
 * 
 * This code 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 code 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 code. If not, see http://www.gnu.org/licenses.
 * 
 * Any errors found in this code should be assumed to be the responsibility of
 * the author of the modifications to the original code (viz. Manuel Menezes de
 * Sequeira).
 */

public class Main {
    private static <Item extends Comparable<? super Item>> int sort(
            final Item[] values, final Item[] auxiliary, final int first,
            final int last) {
        if (first >= last)
            return 0;

        final int middle = first + (last - first) / 2;

        int numberOfInversions = sort(values, auxiliary, first, middle);
        numberOfInversions += sort(values, auxiliary, middle + 1, last);

        numberOfInversions += merge(values, auxiliary, first, middle, last);

        return numberOfInversions;
    }

    private static <Item extends Comparable<? super Item>> int merge(
            final Item[] values, final Item[] auxiliary, final int first,
            final int middle, final int last) {
        int numberOfInversions = 0;

        int i = first;
        int j = middle + 1;
        int k = first;
        for (; i <= middle && j <= last; k++)
            if (!isLess(values[j], values[i])) {
                auxiliary[k] = values[i];
                i++;
            } else {
                auxiliary[k] = values[j];
                j++;
                numberOfInversions += middle - i + 1;
            }

        for (; i <= middle; k++, i++)
            auxiliary[k] = values[i];

        for (i = first; i < j; i++)
            values[i] = auxiliary[i];

        return numberOfInversions;
    }

    private static <Value extends Comparable<? super Value>> boolean isLess(
            final Value first, final Value second) {
        return first.compareTo(second) < 0;
    }
}

Related

  1. intArrayToShortArraySorted(int[] source)
  2. multiQuickSort(int[]... arrays)
  3. quickSort(int[] arr, int startIndex, int endIndex)
  4. radixSort(int[] vs)
  5. selectionSort(int[] arr)
  6. sort( float[] array)
  7. sort(byte[] b, int pos)
  8. sort(double s[], int idx[])
  9. sort(double[] a, int[] b)