Summary of various sorting algorithms 1. Basic concepts of sorting
Sorting is an important operation in computer programming. Its function is to rearrange a collection or sequence of data elements into a sequence sorted by a specific value of the data element.
Sequences with N records {R1, R2 ,..., Rn}. The sequence of corresponding keywords is {K1, K2 ,..., KN}, the corresponding subscript sequence is 1, 2 ,..., N. By sorting, it is required to find the current subscript sequence 1, 2 ,..., An arrangement of N: P1, P2 ,..., Pn, so that the corresponding keywords meet the following non-decreasing (or non-incrementing) Relationship: kp1 ≤ kp2 ≤... ≤Kpn, and a record sequence ordered by keywords {rp1, RP2 ,..., RPN }.
The data item used as the sort basis is called the "Sort code", that is, the key code of the data element. If the key code is the primary key code, the result obtained after sorting is unique for any sequence to be sorted. If the key code is a secondary key code, the sorting result may not be unique.
There are two basic operations for sorting:
(1) The size of the two keywords in the "Compare" sequence;
(2) "move" records.
If you use a sorting method to sort any data element sequence by key code: if the location relationship between the elements of the same key code is the same before and after sorting, this sorting method is stable, but not necessarily consistent.
Ii. Sort insert categories
1. Insert sorting directly
Direct insertion sorting is the simplest sort of insertion classes. Tables with only one record are always ordered. Therefore, for tables with N records, you can insert data to the ordered table one by one from the second record to the nth record, in this way, a table with N records sorted by key code is obtained.
It uses sequential lookup to sort inserts that locate the Insert Location of R [I] in R [1 .. I-1.
Note the following three key points:
(1) from the R [I-1] Forward order search, monitoring whistle set in R [0];
R [0] = R [I]; // set "Sentinel" for (j = I-1; R [0]. key <R [J]. key; -- j) // return J + 1 from the back; // return R [I] With the insert position J + 1
(2) For records whose keywords are not smaller than R [I]. Key found during the search process, you can move them backward while searching, that is, searching and moving are performed simultaneously.
for (j=i-1; R[0].key<R[j].key; --j) { R[j+1] = R[j]; }
(3) I = 2, 3 ,..., N to sort the entire sequence (starting from I = 2 ).
[Algorithm]
// C ++ Code to ensure that void insertionsort (int * r, int length) {for (INT I = 2; I <= length; ++ I) can be run) {R [0] = R [I]; // set to Int J; For (j = I-1; R [0] <R [J]; -- J) {R [J + 1] = R [J]; // search for edge and move back} R [J + 1] = R [0]; // insert to the correct position }}
[Performance Analysis]
(1) space efficiency: Only one auxiliary unit is used, and the space complexity is O (1 ). Only R [0] is needed for assistance.
(2) time efficiency: insert records to an ordered table one by one and perform n-1 queries. Each operation is divided into key codes and moving records, the number of comparisons and the number of records to be moved depend on the initial arrangement of the columns to be sorted by the key code.
The time complexity of direct insertion sorting is O (n), and the average time complexity is O (n ^ 2 ).
(3) Stability: directly inserting sorting is a stable sorting method.
In general, direct insertion sorting is more suitable for the case where the number of sorting is small and the sorting is basically ordered.
2. Semi-insert sorting
The basic operation of directly inserting sorting is to insert a record into the ordered table. The insertion position is determined by comparing the records in the ordered table by key codes one by one. On average, the total number of comparisons is about (N ^ 2)/4. Since the insert position is determined in an ordered table, the insert position can be determined continuously in a binary ordered table, that is, a comparison. The key code is used to compare the records to be inserted with the records centered in the ordered table, split an ordered table into two parts. The next comparison is performed in one of the ordered sub-tables, and the sub-tables are split into two parts. This continues until only one record exists in the subtable to be compared, and the insert position is determined after the comparison.
Semi-insertion sorting is to use semi-lookup to achieve "finding the insert position of R [I] in R [1 .. I-1 ".
To sum up, semi-insert sorting only reduces the number of comparisons, so the total time complexity of semi-insert sorting is still O (N ^ 2 ).
3. Hill sorting
Hill sorting, also known as downgrading incremental sorting, is greatly improved compared with direct insertion sorting and semi-insertion sorting. The direct insertion sorting algorithm is simple. When the N value is small, the efficiency is relatively high. When the N value is large, if the sequence is basically ordered by the key code, the efficiency is still relatively high, the time efficiency can be increased to O (n ). Hill sorting is based on these two points and provides an improved method for inserting sorting.
The basic idea of hill sorting is to divide the sequence of the records to be sorted into several "sparse" subsequences for direct insertion and sorting respectively. After the rough adjustment above, the records in the entire sequence are basically ordered, and then all records are directly inserted and sorted once. In specific implementation, first select the distance between the two records D1, in the entire sequence of the records to be sorted, all the records separated by D1 are divided into a group, in which the group is directly inserted and sorted, then, take the distance D2 <d1 between the two records. In the entire sequence of records to be sorted, divide all records separated by D2 into a group and insert and sort the records in the group directly, until the distance between two records is Dt = 1, there is only one subsequence, that is, the entire sequence of records to be sorted.
[Performance Analysis]
(1) space efficiency: Only one auxiliary unit is used, and the space complexity is O (1 ).
(2) time efficiency: It is difficult to analyze the Time Effectiveness of hill sorting. The comparison times and the number of records of key codes depend on the selection of step-size factor sequences, you can accurately estimate the number of comparison times and the number of movements of records for a specific case. No one has provided a method to select the best step-size factor sequence. Step-Size Factor sequences can have various extraction methods, such as odd numbers and prime numbers. However, note that step-size factors do not have a common factor except 1, the last step must be 1.
O (log2n )~ A value between O (N ^ 2.
(3) Stability: The Hill sorting method is an unstable sorting method.
Iii. Exchange sorting
The key codes to be sorted are compared by two pairs. If the key codes are reversed with the sorting requirements, the key codes are exchanged.
1. Bubble Sorting (adjacent comparison method)
Bubble Sorting is the simplest type of exchange sorting.
Assume that the status of the record sequence R [1. N] is:
Then, the basic idea of the I-th bubble insertion sorting is: "exchange" the records in the disordered sequence, switches the record with the largest keyword in the unordered sequence to the position of R [n-I + 1.
[Algorithm]
// C ++ code void bubblesort (int * r, int length) {bool change = true; For (INT I = 0; I! = Length-1 & change; ++ I) {change = false; For (Int J = 0; J! = Length-i-1; ++ J) {If (R [J]> r [J + 1]) // if a large number of adjacent elements are in front, exchange {int temp = R [J]; R [J] = R [J + 1]; R [J + 1] = temp; change = true ;}}}}
[Performance Analysis]
(1) space efficiency: Only one auxiliary unit is used, and the space complexity is O (1 ).
(2) time efficiency: the best time complexity is O (n), and the average time complexity is O (n ^ 2 ).
(3) Stability: The Bubble sorting method is a stable sorting method.
Total number of comparisons