1, when arranging elements with select Sort, the elements need to be compared, so the comparable<t> interface needs to be implemented. That is, <t extends Comparable<t>>. Further, if the type to be compared can be compared with its parent type, it needs to be written as: <t extends comparable<? Super T>, wherein <? Super T> represents any super class of T.
2,selectionsortarray.java implements the iterative form and recursive form of the selection sort. The specific code is as follows:
Public classSelectionsortarray {/** Task: Arranges the first n objects in the array in ascending order * @param an array of comparable objects * @param n greater than 0 integers representing the length of the array*/ Public Static<textendscomparable<?SuperT>>voidSelectionsort (t[] A,intN) { for(intindex = 0; Index < n-1; index++){ intIndexofnextsmallest = Getindexofsmallest (A, index, n-1); Swap (A, index, indexofnextsmallest); } } //returns the index of the smallest value between index first and last index Private Static<textendscomparable<?SuperT>>intGetindexofsmallest (t[] A,intFirstintLast ) {T min=A[first]; intIndexofmin =First ; for(intindex = first + 1; Index <= last; index++){ if(A[index].compareto (min) < 0) {min=A[index]; Indexofmin=index; }//End If } returnindexofmin; } //the interchange array element does not involve the CompareTo method, and therefore uses object as the element type Private Static voidSwap (object[] A,intIintj) {Object temp= A[i];//The exchange is not a reference, but a specific element's valueA[i] =A[j]; A[J]=temp; } //recursive method for selecting sorting Public Static<textendscomparable<?SuperT>>voidSelectionsort_recursive (t[] A,intN) {Selectionsort (A,0, N-1); } Private Static<textendscomparable<?SuperT>>voidSelectionsort (t[] A,intFirstintLast ) { if(First <Last ) { intIndexofnextsmallest =Getindexofsmallest (A, first, last); Swap (A, first, indexofnextsmallest); Selectionsort (A, first+ 1, last); } }}
Java implementation of selecting sorting algorithm