[3] Exchange Sort
Basic thinking is: Two-two comparison of key codes to be sorted, if in reverse order, exchange it until all objects are ranked.
Bubbling sort
Bubbling Sort is a comparison of two records, and the reverse order is exchanged. Such a method leads to a small key code layer, so the name is obtained. 9CBS Forum once discussed "bubble" and "bubble" are not a thing. It seems that this is a disaster that translators. The English name is Bubble Sort, and it can be in the case of being written. . (Strict version is from the back, Yin version is from the back of the back, it is good to translate the two books as "bubble sort", otherwise it is as the conclusion of some people - one is from The front row, one is from going to the back row)
Template
Void Bubblesort (T A [], INT N, INT & KCN, INT & RMN)
{
KCN = 0; RMN = 0; bool exchange = true;
For (int i = 1; i For (int J = n - 1; j> = i; j -) { EXCHANGE = FALSE; IF ( KCN && A [J - 1]> A [J]) {SWAP (A [J - 1], A [J]); Exchange = True; RMN = 3;} } } It should be noted that don't write it below, although the result is correct: Template Void Bubblesort2 (T A [], INT N) { For (int i = 0; i For (int J = 1; j IF (A [J - 1]> A [J]) SWAP (A [J - 1], A [J]); } Test Results: Sort ascending n = 10000 timeespared: 0ms KCN = 9999 kcn / n = 0.9999 kcn / n ^ 2 = 9.999e-005 kcn / nlogn = 0.07525 RMN = 0 RMN / N = 0 RMN / N ^ 2 = 0 RMN / NLOGN = 0 Sort randomness n = 10000 timeespared: 1161ms KCN = 45409094 kcn / n = 4540.91 kcn / n ^ 2 = 0.454091 KCN / NLOGN = 341.737 RMN = 71526984 RMN / N = 7152.7 RMN / N ^ 2 = 0.71527 RMN / NLOGN = 538.294 Sort descending n = 10000 timeespared: 1022ms KCN = 49995000 kcn / n = 4999.5 kcn / n ^ 2 = 0.49995 kcn / nlogn = 376.25 RMN = 149985000 RMN / N = 14998.5 RMN / N ^ 2 = 1.49985 RMN / NLOGN = 1128.75 It can be seen that the efficiency is very poor, it is better to sort, I really don't know why people are here, is it to understand rapid sorting? There is another interesting phenomenon. Although the reverse sequence KCN and RMN are more than the downtime, the time in the reverse sequence is less than the chaos, from here you can see the role of the CPU pipeline, here you can give us a signal, one Really good algorithms need to make full use of hardware characteristics. When the number of records (n = 1000000), it can be seen that in the case of complete or order, the foaming is better than the insertion, as mobile records are required at this time. Quick sort It's really sorrowful for this algorithm, even a name that can indicate the substance of the algorithm (such as straight insertion, table plug) is not, not like Hill sorting is named by inventors, is it because it is too fast? Perhaps "fast" is the highest honor of sorting algorithms. Basic ideas are: one record of serving sequence as a reference, according to this key code size, divide the entire sequence into two sequences - all records of all records on the left are smaller than the reference small (or etc.), right side Both are larger than the benchmark, the baseline is placed between two subsequences, apparent that the reference is placed in the last position. The above procedure is repeated separately from the left and right subsequences, and all records are placed in the corresponding position. The following routines are not easy to understand, because this is a few improvements: Template INT Partition (T a [], int LEFT, INT RIGHT, INT & KCN, INT & RMN) { INT pivotpos = left; t pivot = a [left]; // pivot For (int i = left 1; i <= right; i ) IF ( KCN && A [i] {SWAP (a [i], a [pivotpos]); RMN = 3; SWAP (a [left], a [pivotpos]); RMN = 3; Return Pivotpos; } The calculated pivot position is used as a function to avoid the use of useless temporary variables when recursive. When you decide to use recursive, pay attention to this - put everything can be placed outside. Note how this function reaches our "The left side is smaller than it, the right side is bigger than it". Template Void QSrecurve (T a [], int LEFT, INT RIGHT, INT & KCN, INT & RMN) { IF (Left { INT Pivotpos = Partition QSrecurve QSrecurve } } Template Void Quicksort (T A [], INT N, INT & KCN, INT & RMN) { KCN = 0; RMN = 0; QSrecurve } These two can only be considered a housing, especially the last one. Test Results: Sort ascending n = 10000 Timespared: 1051ms KCN = 49995000 kcn / n = 4999.5 kcn / n ^ 2 = 0.49995 kcn / nlogn = 376.25 RMN = 29997 rmn / n = 2.9997 RMN / N ^ 2 = 0.0002997 RMN / NLOGN = 0.22575 Sort randomness n = 10000 timeespared: 0ms KCN = 155655 kcn / n = 15.5655 kcn / n ^ 2 = 0.00155655 KCN / NLOGN = 1.17142 RMN = 211851 RMN / N = 21.1851 RMN / N ^ 2 = 0.00211851 RMN / NLOGN = 1.59434 Sort descending n = 10000 timeespared: 1082ms KCN = 49995000 kcn / n = 4999.5 kcn / n ^ 2 = 0.49995 kcn / nlogn = 376.25 RMN = 29997 rmn / n = 2.9997 RMN / N ^ 2 = 0.0002997 RMN / NLOGN = 0.22575 It can be seen that the average performance is very good, but the performance at both ends is not as straight. The case of testing n = 100000 is as follows (Million Remember to comment out the test and reverse test, otherwise, "crash" not to find me) Sort randomness n = 100000 Timespared: 110ms KCN = 2123221 KCN / N = 21.2322 kcn / n ^ 2 = 0.000212322kcn / nlogn = 1.27831 RMN = 3010848 RMN / N = 30.1085 RMN / N ^ 2 = 0.000301085RMN / NLOGN = 1.81271 It is really very "fast", but its worst situation is really unfameful, in case ..., and because of the use of stack recursive, the worst case is not allowed to collapse. In order to reduce this adverse tendency, the improved approach is "Triple Taking", that is, the first, last, one of the key code hosted in the middle of the sequence to be sorted as the reference. Just change the partition function. Template INT Partition (T a [], int LEFT, INT RIGHT, INT & KCN, INT & RMN) { INT MID = (LEFT RIGHT) / 2; IF ( KCN && A [Left]> a [MID]) { IF ( KCN && A [Left]> a [Right]) { IF ( KCN && A [MID]> A [Right]) {swap (a [MID], A [Left]); RMN = 3;} Else {swap (a [right], a [left]); RMN = 3;} } } Else { IF ( KCN && A [LEFT]
{