Find the smallest Comparable in a subarray determined by indices first and last. - Java Collection Framework

Java examples for Collection Framework:Array Sub Array

Description

Find the smallest Comparable in a subarray determined by indices first and last.

Demo Code


//package com.java2s;

public class Main {
    /** Find the smallest Comparable in a subarray determined by
     * indices first and last./*from   w  w  w.j ava 2s .c o m*/
     * 
     * @param a  the array
     * @param first  the index of the first element of the subarray
     * @param last  the index of the last element of the subarray
     * @return the index of the smallest element of the subarray */
    public static <T extends Comparable<? super T>> int getIndexOfSmallest(
            T[] a, int first, int last) {
        int smallest = first;
        for (int index = first + 1; index <= last; index++) {
            if (a[smallest].compareTo(a[index]) == 1)
                smallest = index;
        }
        return smallest;
    }
}

Related Tutorials