Java implementation of selecting sorting algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.