Java examples for Collection Framework:Array Sub Array
Given array indices that define a sorted subarray, place the element just above the subarray at the end of the subarray if it is smaller than the largest element and recurse on a 1-smaller subarray.
//package com.java2s; public class Main { /** Given array indices that define a sorted subarray, place the element * just above the subarray at the end of the subarray if it is smaller * than the largest element and recurse on a 1-smaller subarray. * /*from www. jav a 2 s . c o m*/ * @param a the array to work on * @param begin the index of the beginning of the sorted subarray * @param end the index of the end of the sorted subarray */ public static <T extends Comparable<? super T>> void insertInOrderRecursive( T[] a, int begin, int end) { /* pre: end is not the last element of a */ if (begin <= end) { if (a[end + 1].compareTo(a[end]) == -1) { swap(a, end, end + 1); insertInOrderRecursive(a, begin, end - 1); } } } /** Swap two elements of an array given their indices. * * @param a the array to work on * @param first the index of the first element to swap * @param second the index of the second element to swap */ public static void swap(Object[] a, int first, int second) { Object newFirst = a[second]; a[second] = a[first]; a[first] = newFirst; } }