basic idea: first, find the smallest element in the array, and then swap it with the first element of the group (if the first element is the smallest element then it swaps with itself). Again, find the smallest element in the remaining element and swap it with the second element of the array. This is repeated until the entire array is sorted.
Packagecom.test;Importjava.util.Arrays; Public classSelectsort { Public Static voidMain (string[] args) {intTest[] = {49,38,65,97,76,13,27,49}; Sort (test); System.out.println (arrays.tostring (test)); } Public Static voidSortint[] a) { for(inti = 0; i < a.length; i + +) { intmin = i;//define the current subscript as the minimum subscript intTMP =A[i]; for(intj = i + 1; J < A.length; J + +) { if(A[j] < TMP) {//A[j] < temp sort from small to large; A[j] > Temp from big to smallTMP = A[j];//If there is a keyword less than the current minimum value, assign the subscript of this keyword to minMin =J; } } if(min! =i) {A[min]=A[i]; A[i]=tmp; } } }}
Java selection Sort