Sorting Algorithm Summary: directly select sorting and Sorting Algorithm Summary
Concept
Take the smallest record in the last n-I + 1 (I =,..., n-1) as the I record of the ordered table.
Dynamic Effect:
Advantage: the algorithm is simple and easy to implement.
Disadvantage: only one element can be determined at a time.
Java implementation:
Package com. liuhao. sort; import java. util. arrays; // defines a data packaging class DataWrap implements Comparable <DataWrap> {int data; String flag; public DataWrap (int data, String flag) {this. data = data; this. flag = flag;} public String toString () {return data + flag;} @ Override public int compareTo (DataWrap dw) {return this. data> dw. data? 1: (this. data = dw. data? 0:-1) ;}} public class SelectSort {public static void selectSort (DataWrap [] data) {System. out. println ("start sorting"); int arrayLength = data. length; // the n-1 comparison is performed sequentially. the I-th comparison selects the I-th value and places it on the I position for (int I = 0; I <arrayLength-1; I ++) {for (int j = 1 + 1; j <arrayLength; j ++) {// data on I> data on j if (data [I]. compareTo (data [j])> 0) {DataWrap tmp = data [I]; data [I] = data [j]; data [j] = tmp;} System. out. println ("Number" + (I + 1) + "after sorting:" + Arrays. toString (data) ;}} public static void main (String [] args) {DataWrap [] data = {new DataWrap (21, ""), new DataWrap (30, ""), new DataWrap (49, ""), new DataWrap (30, "*"), new DataWrap (16, ""), new DataWrap (9, "")}; System. out. println ("Before sorting:" + Arrays. toString (data); selectSort (data); System. out. println ("sorted:" + Arrays. toString (data ));}}
Run the above program to see the sorting effect:
You only need to select the smallest data for sorting each trip and put it in the first place. You can find that each trip only needs to be exchanged once. The above algorithms are exchanged more than once in each comparison.
Improved Algorithm:
// The n-1 comparison is performed in sequence, and the I-th comparison selects the I-th value and places it on the I position for (int I = 0; I <arrayLength-1; I ++) {// minIndex is used to retain the index int minIndex = I; for (int j = 1 + 1; j <arrayLength; j ++) of the minimum value in this trip) {// data on I> data on j if (data [minIndex]. compareTo (data [j])> 0) {minIndex = j ;}} if (minIndex! = I) {DataWrap tmp = data [I]; data [I] = data [minIndex]; data [minIndex] = tmp;} System. out. println ("Number" + (I + 1) + "after sorting:" + Arrays. toString (data ));}
The purpose of each comparison is to find the index (minIndex) of the smallest data in this section ).
For direct sorting, the maximum number of data exchanges is n-1, but the number of comparisons is large. The time complexity is O (n2), and the space complexity is only O (1 ).
Judging from the sorting results of the preceding two DataWrap with data of 30, Direct selection of sorting is unstable.
What is the idea of selecting a sorting algorithm?
Select the maximum value of the current sequence for each time:
For example, 1, 3, 6, 9, and 5 are sorted in ascending order!
Step 1
Step 2, 1
Step 3, 3, 5
Step 4, 1, 5, 6
Step 5, 1, 5, 6, 9
// Progressively select the implementation of sorting!
Void SelectSort (double * head, int amount)
{
Double temp;
Int m, n;
For (m = 0; m <amount-1; m ++)
{
For (n = m + 1; n <amount; n ++)
{
If (head [n] {
Temp = head [n];
Head [n] = head [m];
Head [m] = temp;
}
}
}
}
Direct selection of sorting algorithms
The addition and non-addition operations are the same. With this statement, R [m] is the smallest after the for loop above. If R [I] is not the smallest, that is, m! = I, so we need to switch. After the switch, R [I] is the smallest. if R [I] is the smallest, if (m! = I) the following statement can be executed without being executed, and the exchange is not executed.