Here you can find the source of sort(Object[] array, int start, int end)
Parameter | Description |
---|---|
array | the Object array to be sorted |
start | the start index to sort |
end | the last + 1 index to sort |
@SuppressWarnings("unchecked") public static void sort(Object[] array, int start, int end)
//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); } }