Java Array Sort multiQuickSort(int[]... arrays)

Here you can find the source of multiQuickSort(int[]... arrays)

Description

Multi-sorts the given arrays with the quicksort algorithm.

License

Apache License

Declaration

public static void multiQuickSort(int[]... arrays) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Arrays;

public class Main {
    /**//  w w w .  j  a va  2  s  .c  o m
     * Multi-sorts the given arrays with the quicksort algorithm. It assumes that
     * all arrays have the same sizes and it sorts on the first dimension of these
     * arrays. If the given arrays are null or empty, it will do nothing, if just
     * a single array was passed it will sort it via {@link Arrays} sort;
     */
    public static void multiQuickSort(int[]... arrays) {
        multiQuickSort(0, arrays);
    }

    /**
     * Multi-sorts the given arrays with the quicksort algorithm. It assumes that
     * all arrays have the same sizes and it sorts on the given dimension index
     * (starts with 0) of these arrays. If the given arrays are null or empty, it
     * will do nothing, if just a single array was passed it will sort it via
     * {@link Arrays} sort;
     */
    public static void multiQuickSort(int sortDimension, int[]... arrays) {
        // check if the lengths are equal, break if everything is empty
        if (arrays == null || arrays.length == 0) {
            return;
        }
        // if the array only has a single dimension, sort it and return
        if (arrays.length == 1) {
            Arrays.sort(arrays[0]);
            return;
        }
        // also return if the sort dimension is not in our array range
        if (sortDimension < 0 || sortDimension >= arrays.length) {
            return;
        }
        // check sizes
        int firstArrayLength = arrays[0].length;
        for (int i = 1; i < arrays.length; i++) {
            if (arrays[i] == null || firstArrayLength != arrays[i].length)
                return;
        }

        multiQuickSort(arrays, 0, firstArrayLength, sortDimension);
    }

    /**
     * Internal multi quicksort, doing the real algorithm.
     */
    private static void multiQuickSort(int[][] a, int offset, int length, int indexToSort) {
        if (offset < length) {
            int pivot = multiPartition(a, offset, length, indexToSort);
            multiQuickSort(a, offset, pivot, indexToSort);
            multiQuickSort(a, pivot + 1, length, indexToSort);
        }
    }

    /**
     * Multi-sorts the given arrays with the quicksort algorithm. It assumes that
     * all arrays have the same sizes and it sorts on the first dimension of these
     * arrays. If the given arrays are null or empty, it will do nothing, if just
     * a single array was passed it will sort it via {@link Arrays} sort;
     */
    @SafeVarargs
    public static <T extends Comparable<T>> void multiQuickSort(T[]... arrays) {
        multiQuickSort(0, arrays);
    }

    /**
     * Multi-sorts the given arrays with the quicksort algorithm. It assumes that
     * all arrays have the same sizes and it sorts on the given dimension index
     * (starts with 0) of these arrays. If the given arrays are null or empty, it
     * will do nothing, if just a single array was passed it will sort it via
     * {@link Arrays} sort;
     */
    @SafeVarargs
    public static <T extends Comparable<T>> void multiQuickSort(int sortDimension, T[]... arrays) {
        // check if the lengths are equal, break if everything is empty
        if (arrays == null || arrays.length == 0) {
            return;
        }
        // if the array only has a single dimension, sort it and return
        if (arrays.length == 1) {
            Arrays.sort(arrays[0]);
            return;
        }
        // also return if the sort dimension is not in our array range
        if (sortDimension < 0 || sortDimension >= arrays.length) {
            return;
        }
        // check sizes
        int firstArrayLength = arrays[0].length;
        for (int i = 1; i < arrays.length; i++) {
            if (arrays[i] == null || firstArrayLength != arrays[i].length)
                return;
        }

        multiQuickSort(arrays, 0, firstArrayLength, sortDimension);
    }

    /**
     * Internal multi quicksort, doing the real algorithm.
     */
    private static <T extends Comparable<T>> void multiQuickSort(T[][] a, int offset, int length, int indexToSort) {
        if (offset < length) {
            int pivot = multiPartition(a, offset, length, indexToSort);
            multiQuickSort(a, offset, pivot, indexToSort);
            multiQuickSort(a, pivot + 1, length, indexToSort);
        }
    }

    /**
     * Partitions the given array in-place and uses the end element as pivot,
     * everything less than the pivot will be placed left and everything greater
     * will be placed right of the pivot. It returns the index of the pivot
     * element after partitioning. This is a multi way partitioning algorithm, you
     * have to provide a partition array index to know which is the array that
     * needs to be partitioned. The swap operations are applied on the other
     * elements as well.
     */
    private static int multiPartition(int[][] array, int start, int end, int partitionArrayIndex) {
        final int ending = end - 1;
        final int x = array[partitionArrayIndex][ending];
        int i = start - 1;
        for (int j = start; j < ending; j++) {
            if (array[partitionArrayIndex][j] <= x) {
                i++;
                for (int[] anArray : array) {
                    swap(anArray, i, j);
                }
            }
        }
        i++;
        for (int[] anArray : array) {
            swap(anArray, i, ending);
        }

        return i;
    }

    /**
     * Partitions the given array in-place and uses the end element as pivot,
     * everything less than the pivot will be placed left and everything greater
     * will be placed right of the pivot. It returns the index of the pivot
     * element after partitioning. This is a multi way partitioning algorithm, you
     * have to provide a partition array index to know which is the array that
     * needs to be partitioned. The swap operations are applied on the other
     * elements as well.
     */
    private static <T extends Comparable<T>> int multiPartition(T[][] array, int start, int end,
            int partitionArrayIndex) {
        final int ending = end - 1;
        final T x = array[partitionArrayIndex][ending];
        int i = start - 1;
        for (int j = start; j < ending; j++) {
            if (array[partitionArrayIndex][j].compareTo(x) < 0) {
                i++;
                for (T[] anArray : array) {
                    swap(anArray, i, j);
                }
            }
        }
        i++;
        for (T[] anArray : array) {
            swap(anArray, i, ending);
        }

        return i;
    }

    /**
     * Swaps the given indices x with y in the array.
     */
    public static void swap(int[] array, int x, int y) {
        int tmpIndex = array[x];
        array[x] = array[y];
        array[y] = tmpIndex;
    }

    /**
     * Swaps the given indices x with y in the array.
     */
    public static void swap(long[] array, int x, int y) {
        long tmpIndex = array[x];
        array[x] = array[y];
        array[y] = tmpIndex;
    }

    /**
     * Swaps the given indices x with y in the array.
     */
    public static void swap(double[] array, int x, int y) {
        double tmpIndex = array[x];
        array[x] = array[y];
        array[y] = tmpIndex;
    }

    /**
     * Swaps the given indices x with y in the array.
     */
    public static void swap(boolean[] array, int x, int y) {
        boolean tmpIndex = array[x];
        array[x] = array[y];
        array[y] = tmpIndex;
    }

    /**
     * Swaps the given indices x with y in the array.
     */
    public static <T> void swap(T[] array, int x, int y) {
        T tmpIndex = array[x];
        array[x] = array[y];
        array[y] = tmpIndex;
    }
}

Related

  1. getLeftIndex(float[] sorted, float value)
  2. getMedianIndex(float[] sorted, float value)
  3. getNewSortedIntArray(int[] codePointArray)
  4. getSortedDistinct(int[] values)
  5. intArrayToShortArraySorted(int[] source)
  6. quickSort(int[] arr, int startIndex, int endIndex)
  7. radixSort(int[] vs)
  8. selectionSort(int[] arr)
  9. sort( final Item[] values, final Item[] auxiliary, final int first, final int last)