Today, Dapeng learned how to directly select the sorting method in sorting.
Straight selectsort is also a simple sorting method. Its basic idea is to first select the minimum value from R [0]-R [n-1, exchange with R [0], select the minimum value from R [1]-R [n-1] For the second time, and exchange with R [1 ,... A total of N-1 exchanges are used to obtain an ordered sequence of sorted codes in ascending order.
The Java implementation code is as follows:
Public classStraightselectsort {
Public static voidMain (string [] ARGs ){
//TodoAuto-generatedmethod stub
Int[] A = };
IntI, j, index;
System.Out. Print ("Before sorting :");
For(IntX: ){
System.Out. Print (x + ",");
}
For(I = 0; I <A. length; I ++ ){
Index = I;
For(J = I + 1; j <A. length; j ++ ){
If(A [J] <A [Index])
Index = J;
If(I! = Index ){
IntTMP = A [I];
A [I] = A [Index];
A [Index] = TMP;
}
}
}
System.Out. Print ("sorted :");
For(IntX: ){
System.Out. Print (x + ",");
}
}
}
Regardless of the initial status of the sequence, n-I comparisons are required to select the maximum and minimum keyword records in the I-th sorting. Therefore, the total number of comparisons is n (n-1) /2 = O (N ^ 2 ).
Select sort directly select sort