public static void Selectsort (int[] a) {if (a = = null) {return;} int n = a.length;int Minindex = 0;//A minimum number from the unordered area is placed at the end of the ordered area, initially thinking that the entire array is unordered, choosing a minimum number to place the first for (int i=0; i<n; i++) {Minindex = i;for (int j=i+1; j<n; J + +) {if (A[j]<a[minindex]) {minindex = j;}} Swap position, place the smallest element in the unordered area to the last int temp = a[i];a[i] = A[minindex];a[minindex] = temp in the ordered area; }}
Similar to the direct selection sort and insert sort, the data is divided into ordered and unordered areas.
The difference is that the insertion sort is to insert the first element of the unordered area directly into the ordered area, initially by default the first element is ordered, step by step to reduce the range of unordered extents.
Selecting a sort is to first select the smallest element in the unordered area and then place it in the first position behind the ordered area.
At the same time, the choice of sorting is also an unstable sort, for example, the sequence of 4,3,4,1,2, the first exchange will be 4 and 1 exchange, then the original sequence of the relative order of two 4 is destroyed.
java-Select Sort