PackageCom.czj; Public classSelectsort {/*** Select sort to sort the Sortarray incoming array from small to large. * The idea is to find the smallest value in the array, put it in the first position of the array, and then find the second smallest value, and place it in the second position *@paramSortarray*/ Public voidSelectsort (int[] sortarray) { intLast=sortarray.length;//Last array length for(inti=0;i<last;i++) {//iterating through an array of loops intCurrentmin=sortarray[i];//set the first value of the array to the minimum intCurrentminindex=i;//Record the subscript//find values that are smaller than the minimum value set, record values and subscript for(intj=i;j<last;j++){ if(Currentmin>sortarray[j]) {//Here you can change the array from large to small by changing the greater than sign .currentmin=Sortarray[j]; Currentminindex=J; } } //To switch the minimum value to the front, if(currentminindex!=i) {Sortarray[currentminindex]=Sortarray[i]; Sortarray[i]=currentmin; } } } Public Static voidMain (string[] args) {int[] sortnum={1,5,2,7,4,9,345,234}; Selectsort Selectsort=NewSelectsort (); Selectsort.selectsort (Sortnum); for(inti:sortnum) {System.out.print (i+","); } } }
Learn some Java_java every day select sort