Java Array Sort sort(Object[] array, int start, int end)

Here you can find the source of sort(Object[] array, int start, int end)

Description

Sorts the specified range in the array in ascending order.

License

Open Source License

Parameter

Parameter Description
array the Object array to be sorted
start the start index to sort
end the last + 1 index to sort

Declaration

@SuppressWarnings("unchecked")
public static void sort(Object[] array, int start, int end) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2003, 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from  w  w w .  j a  va  2  s .c  o m
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Sorts the specified range in the array in ascending order.
     *
     * @param      array   the Object array to be sorted
     * @param      start   the start index to sort
     * @param      end      the last + 1 index to sort
     *
     * @exception   ClassCastException when an element in the array does not
     *            implement Comparable or elements cannot be compared to each other
     * @exception   IllegalArgumentException when <code>start > end</code>
     * @exception   ArrayIndexOutOfBoundsException when <code>start < 0</code>
     *            or <code>end > array.size()</code>
     */
    @SuppressWarnings("unchecked")
    public static void sort(Object[] array, int start, int end) {
        int middle = (start + end) / 2;
        if (start + 1 < middle)
            sort(array, start, middle);
        if (middle + 1 < end)
            sort(array, middle, end);
        if (start + 1 >= end)
            return; // this case can only happen when this method is called by the user
        if (((Comparable<Object>) array[middle - 1]).compareTo(array[middle]) <= 0)
            return;
        if (start + 2 == end) {
            Object temp = array[start];
            array[start] = array[middle];
            array[middle] = temp;
            return;
        }
        int i1 = start, i2 = middle, i3 = 0;
        Object[] merge = new Object[end - start];
        while (i1 < middle && i2 < end) {
            merge[i3++] = ((Comparable<Object>) array[i1]).compareTo(array[i2]) <= 0 ? array[i1++] : array[i2++];
        }
        if (i1 < middle)
            System.arraycopy(array, i1, merge, i3, middle - i1);
        System.arraycopy(merge, 0, array, start, i2 - start);
    }
}

Related

  1. sort(O[] array)
  2. sort(Object[] a)
  3. sort(Object[] arr, int start, int end)
  4. sort(Object[] array)
  5. sort(Object[] array, Comparator comparator)
  6. sort(Object[] array_, boolean accendingOrder_)
  7. sort(String a[])
  8. sort(String[] colnos)
  9. sort(String[] s)