Summary of various sorting algorithms
[Watermark]
A sorting algorithm is a basic and commonly used algorithm. The sorting algorithm processes a large number of tasks in actual work.
High speed requirements for algorithms.
In general, the performance of the so-called algorithm mainly refers to the complexity of the algorithm, which is generally expressed by the O method. I will
A detailed description is provided.
I would like to give a brief introduction to the sorting algorithm and give an outline for this article.
I will analyze the algorithm from simple to difficult according to the complexity of the algorithm.
The first part is the simple sorting algorithm. later you will see that their common feature is that the algorithm complexity is O (N * N) (because there is no
If the word is used, the superscript and subscript cannot be typed ).
The second part is the advanced sorting algorithm with the complexity of O (Log2 (N )). Here we only introduce one algorithm. There are also several
Algorithms are not discussed here because they involve the concepts of trees and stacks.
The third part is similar to the brain. The two algorithms here are not the best (or even the slowest), but the algorithms themselves compare
It is special and worthy of reference (programming ). At the same time, we can also understand this problem from another perspective.
The fourth part is a post-dinner dessert that I gave to you-a template-based general-purpose quick sorting. Because it is a template function
You can sort any data type (Sorry, some Forum experts mentioned here ).
Now, let's start:
I. Simple Sorting Algorithm
The program is relatively simple, so no comments are added. All programs provide complete Running code, and in my VC Environment
. Because there is no content related to MFC and WINDOWS, there should be no content on borland c ++ platforms.
Problem. The running process is illustrated after the code, and it is helpful for understanding.
1. Bubble Method:
This is the most primitive and well-known slowest algorithm. The origin of his name is because its work seems to be bubbling:
# Include <iostream. h>
Void BubbleSort (int * pData, int Count)
{
Int iTemp;
For (int I = 1; I <Count; I ++)
{
For (int j = Count-1; j> = I; j --)
{
If (pData [j] <pData [J-1])
{
ITemp = pData [J-1];
PData [J-1] = pData [j];
PData [j] = iTemp;
}
}
}
}
Void main ()
{
Int data [] = {10, 9, 8, 7, 6, 5, 4 };
BubbleSort (data, 7 );
For (int I = 0; I <7; I ++)
Cout <data [I] <"";
Cout <"n ";
}
Reverse Order (worst case)
First round: 10, 9, 8, 7-> 10, 9, 7-> 10, 7, 9-> 7, 10, 9, 8 (three exchanges)
Round 2: 7, 10, 9-> 7, 10, 8-> 7, 8, 9 (2 exchanges)
First round: 7, 8, 10, 9-> 7, 8, 9, 10 (switching once)
Cycles: 6
Number of exchanges: 6
Others:
First round:,->, (exchange twice)
Round 2: 7, 8, 10, 9-> 7, 8, 10, 9-> 7, 8, 10, 9 (0 exchanges)
First round: 7, 8, 10, 9-> 7, 8, 9, 10 (switching once)
Cycles: 6
Number of exchanges: 3
We have given the program section above, and now we analyze it: here, the main part that affects our algorithm performance is loop and exchange,
Obviously, the more times, the worse the performance. From the above program, we can see that the number of cycles is fixed, which is 1 + 2 +... + n-1.
The formula is 1/2 * (n-1) * n.
Note that the O method is defined as follows:
If there is a constant K and the starting point n0, so when n> = n0, f (n) <= K * g (n), f (n) = O (g (n )). (Haha, don't say no
Learning Mathematics well is very important for programming mathematics !!!)
Now let's look at 1/2 * (n-1) * n. When K = 1/2, n0 = 1, g (n) = n * n, 1/2 * (n-1) * n <= 1/2 * n = K * g (n ). So f (n)
= O (g (n) = O (n * n ). So the complexity of our program loop is O (n * n ).
Let's look at the exchange. We can see from the table following the program that the two cases share the same loop and the exchange is different. In fact, the exchange itself is the same as the data source
There is a great relationship between the degree of order. when the data is in reverse order, the number of exchanges is the same as the number of cycles (each cycle will be exchanged ),
The complexity is O (n * n ). When the data is in positive order, there will be no exchange. The complexity is O (0 ). It is in the intermediate state in disordered order. This is precisely because
The reason is that we usually compare the algorithm by the number of cycles. <