Choosesort source Program
public class Choosesort {
private static void Choosesort (int [] a) {
for (int i = 0; i < a.length; i++) {
An array loop, assuming that the first element in the data is smaller than the back, and then compared to the data behind it
int k = i;
for (int j = i+1; J <a.length; J + +) {//Data traversal, looking for the smallest array subscript for the data element
if (A[j]<a[k])
K = J;
}
Minimum value and A[i] Exchange
if (i! = k) {
I and k are compared, if not equal, I and J subscript The element is not the same element, then Exchange A[i],a[k] value, ensure that the minimum value is always the first
int temp = A[i];
A[i] = a[k];
A[K] = temp;
}
}
}
public static void Main (string[] args) {
Int[] Test1 = {51, 38, 49, 27, 62, 5, 16}; Test array
Choosesort (test1);
for (int i = 0; i < test1.length; i++) {
System.out.print (Test1[i] + "");
}
}
}
3.4.1.2 Program Run Results:
5 16 27 38 49 51 62
3.4.2 Source program Disclosure
The basic idea of choosing a sort is that each trip (assuming that it is a trip) is processed from n-i+1 data by selecting a minimum (or maximum) of the data as the first data in an ordered sequence. One of the simplest is called direct selection sorting.
Select the data structure of the sorting algorithm:
Array to sort test1 = {51, 38, 49, 27, 62, 5, 16}
Select the sorting algorithm principle:
? The first pass of the direct selection sort is to select a minimum data from all n data in the data series as the 1th element in the ordered sequence and position it in the first storage location. The second pass selects a second small element from the data sequence's n-1 data as the 2nd element in an ordered sequence, locates it in the second storage location, and so on, When the n-1 process selects a smaller element from the remaining 2 elements of the data sequence as the last 2nd element in the ordered sequence and locates it in the penultimate storage location, the entire sorting process is complete.
Java Learning Data-java Common algorithm-selection sorting algorithm