Java examples for Collection Framework:Array Sort
inserts the element at the end of a particular sub-array into its sorted position in the rest of the array before it
//package com.java2s; import java.util.ArrayList; public class Main { /**/*w w w . j a v a 2 s.co m*/ * inserts the element at the end of a particular sub-array into its sorted position in the rest of the array before it * * @param arrayList the ArrayList with an element that you want to insert in order * @param sortedStart the index of the first element in the sorted subset of the array. all elements between this and * $unsortedStart are sorted * @param unsortedStart the index of arrayList that marks the element that you want to insert into the sorted section. * it could already be in the right place, but we're not sure. */ public static <T extends Comparable<? super T>> void insertInOrder( ArrayList<T> arrayList, int sortedStart, int unsortedStart) { T insertElement = arrayList.get(unsortedStart); int insertIndex = unsortedStart; while (insertIndex != sortedStart && insertElement.compareTo(arrayList.get(insertIndex - 1)) == -1) { arrayList.set(insertIndex, arrayList.get(insertIndex - 1)); insertIndex--; } if (insertIndex != unsortedStart) arrayList.set(insertIndex, insertElement); } }