The example of this article tells the selection of the Java sort algorithm summary. Share it for everyone for your reference. The specific analysis is as follows:
The basic operation of selecting sorting is one of the minimum (or maximum) elements selected from the data elements to be sorted, and the order is at the end of the number of sequences until the data elements that are to be sorted. The algorithm is unstable, the extra space of O (1), the comparison time complexity is O (n^2), and the time complexity of the exchange is O (n), which is not adaptive. It is not recommended in most cases. Only when you want to reduce the number of exchanges.
Basic ideas
The direct selection of the file of the n-recorded file can be selected directly through the N-1 trip to get the orderly results:
① Initial state: The disorderly area is R [1..N], and the orderly area is empty.
② The first sort is sorted in the disorderly area R [1..N] to select the minimum record R [k], and exchange it with the first record R [1] in the disorderly area. ..1] and R [2..N] are changed to new ordered areas of the number of records and a new disorderly area with a number of records of 1 and the number of records decreased by 1.
Nympho
③ Sorting II
At the beginning of the sort of the i, the current ordered areas and disorder areas are R [1..i-1] and R (1≤i≤N-1). This sort selected the minimum record R [k] from the current disorderly area, and exchange it with the first record R in the disorderly area, so that R [1..i] and R are separated into records. The number of new ordered areas of the number and a new disorderly area with a number of records decreased by 1.
In this way, the direct selection of the files of the n-recorded file can be directly selected by N-1 trips to get orderly results.
Code implementation
Public class test {public static int [] a = {10,32,1,9,5,7,12,0,4,3}; // Preset data array Public Static void Main (String ARGS []) {) { int i; // Cycling count variable int index = a.length; // Data index variable system.out.print ("" before sort: "); for (i = 0; i </+) system.out .printf ("%3s", a); System.out.println (""); selectsort (index -1); // Select systerm.out.print after sorting // for (i = 0; I <inDex -1; I ++) System.out.printf ("%3s", a); System.out.println (""); , j, k; // cycle count variable int Minvalue; // minimum value variable int int indexmin; // minimum value index variable int Temp; // temporary deposit variable for (i = 0; i <inEx -1; i ++) { Minvalue = 32767; // The current minimum value INDEXMIN = 0; // The index value of the minimum value for storage for (j = i; j <in <inDex; j ++) {if (a [j] <minvalue) // Find the minimum value {minvalue {minvalue = a [j]; // Storage minimum indexmin = j;} temp = a; // exchange two values a = a; a = test;} system.out.print ("sort:"); for (k (k (k (k (k (k (k = 0; k <inDex; k ++) system.out.printf ("%3s", a [k]); System.out.println ("");}}}}}}
Like the bubbling sorting method, the outer layer cycle still needs to perform N-1 times, and its efficiency is still poor.
It is hoped that this article is helpful to everyone's Java program design.