Java Array Merge merge( final Item[] values, final Item[] auxiliary, final int first, final int middle, final int last)

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

Description

merge

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*/*from   w w  w . ja  v  a 2 s .c  o  m*/
 * 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 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. arrayMerge16bit(byte[] sourceArray, int sourceStart, byte[] destinationArray, int destinationStart, int length)
  2. concat(double[] merged, double[] is)
  3. merge(byte[] a, byte[] b)
  4. merge(byte[] array1, byte[] array2)
  5. merge(byte[] signature, byte[] message)
  6. merge(byte[] src, byte[] src2, int start, int length)