9.10 Summary Review
The content of this chapter is just sorting, we need to compare the various sorting algorithms already mentioned to summarize the review.
First we talked about the definition of sorting, and mentioned the stability of the sort, which is critical to some special requirements, so we need to pay attention to the stability of the algorithm in the sort algorithm.
We will sort the records are all placed in memory, the sorting is divided into the sort and the outside sort, the outside sort needs to be exchanged between the inside and outside the data to be carried out repeatedly. In this chapter, we mainly talk about the algorithm of the internal sorting.
According to the main operation of the sorting process, we divide the inner sort into four categories: insert sort, exchange sort, select sort and merge sort. The seven sorts of sorting methods introduced later are the representative algorithms of various classifications.
In fact, there is no perfect sorting algorithm, there are advantages will have shortcomings, even the rapid sorting method, but also in the overall performance of superior, it also has a sort of instability, need a large number of auxiliary space, a small number of data sorting without advantages. So we're going to look at the various sorts of lengths and short that are mentioned from multiple angles.
We compared the various metrics of the seven algorithms, as shown in table 9-10-2.
From the simplicity of the algorithm, we divide seven kinds of algorithms into two categories
1 Simple algorithm: Bubble, simple selection, direct insertion.
2 Improved algorithm: Hill, heap, merge, fast.
On average, it is clear that the three improved algorithms are better than the hill sort, and far outweigh the first three simple algorithms.
In the best case, it's better to bubble and direct the sort, that is, if your to-do list is always basically orderly, you should not consider four complex improvements.
In the worst case scenario, heap sorting and merge sorting are stronger than fast sorting and other simple sorting.
From these three sets of time complexity of the data comparison, we can draw such an understanding. Heap sort and merge sort like two students in the Olympic exam, the psychological quality is strong, play a stable. A quick sort, like a very emotional genius, behaves best in a good mood and becomes passable when confronted with a worse environment. But if they all come to the game to calculate the single digit addition and subtraction, they are not a very ordinary bubble and direct insertion.
In terms of space complexity, the merge sort emphasizes that horses run fast, and they have to feed their horses. The fast sort also has the corresponding space request, instead the heap sorts and so on are the small quantity request, the massive pay, to the space request is O (1). If the software that executes the algorithm is very concerned about the amount of memory used, choosing merge sort and quick sort is not a good decision.
From the stability point of view, merge sort of the top, we also said before, for very care about the stability of the application, merge sort is a good algorithm.
From the number of records to be sorted, the number of N to be sorted is smaller, the more appropriate to use a simple sort method. Conversely, the larger the N, the more appropriate to adopt an improved sorting method. This is why we have added a threshold for quick sort optimization, which is below the threshold for direct insertion sort.
From the data in the table above, it seems that the simple selection sort has the worst performance in three simple sorts, not exactly, for example, if a record has a large amount of information (such as a dozens of-digit keyword), it means that it takes up a lot of storage space, and the more time it takes to move a record, We give a comparison of the moving times of three simple sorting algorithms, as shown in table 9-10-3.
You will find that the simple choice of sorting becomes very advantageous at this time, because it is through a lot of comparison after the choice of clear records to move, targeted. Therefore, for the data volume is not very large, and the record of the key word information of a larger ranking requirements, simple ranking algorithm is dominant. In addition, the amount of keyword information that is recorded has little effect on the four improved algorithms.
In short, from the comprehensive indicators, the optimization of the fast ordering is the best performance ranking algorithm, but different occasions we should also consider using different algorithms to deal with it.
9.11 End Words
After learning the sort, you can feel that our algorithm researchers are in the "seemingly impossible" situation, gradually improve the performance of the sorting algorithm. For the rest of the minutes, let's do a puzzle and feel the possibility of becoming impossible.
How to put the figure 9-11-1 with four paragraphs straight a pen to the nine points together.
People raise their hands very quickly, because most of the students should have seen the problem. Students who have not done a topic usually fall into a small trap, turn around in a nine-point frame and find that it takes at least five straight lines to connect. As a result, to find the answer, you have to think through the limitations of these nine points.
If this is the end of the puzzle, then we will not test everyone. Now my question is how to make three straight lines to connect these nine points together.
At this time, everyone is whispering, in the mind must think, "how can this be." "I came to announce the answer, which is to use a" Z "word line can be a stroke. Perhaps the quickest answer is for children who have not studied mathematics. As adults, we have been framed by other "boxes" of the brain. There is a fundamental axiom in mathematics: two parallel lines never intersect. Another basic assumption in mathematics is that the point has no size. But in reality any point will have a size. Break through this limit, as long as the infinite extension of the "Z" word three lines, nine points must be a pen. Look at Figure 9-11-3.
Some students said that the point in my picture is bigger than the one just now, this does not meet the requirements. I want to have this idea of the classmate, may still not understand what I want to express, in fact, the small black spot is small, it is also size, you can imagine three straight lines long enough, they can connect the nine points.
Don't worry, the topic is not finished, I now ask to only use a straight line to the 9.1 strokes, how to do.
Obviously, everyone's mind has been opened. We can easily find the answer, because as long as we break through the geometry of the "line is not thick" frame, with a very thick line, such as a big brush dipped in ink, draw a thick line will be nine points all contained therein.
It is not impossible to use four paragraphs, three paragraphs, a straight line a stroke of nine points, only temporarily has not found a way. All the inventions in real life are built on the basis of breaking the stereotype of the "box" that the predecessors have identified. This puzzle of course is not to challenge the authority of mathematics, it is just to enlighten us: "All things are possible, but we have not found a way." ”
the end of this chapter, in fact, is the end of the course of data structure. Data structures and algorithms, there are a lot of things that we haven't covered. To really master the data structure and apply it to your work, you have a long way to go.
in our lives, contradictions and puzzles are often accompanied. Many students to learn the data structure, in fact, is not really understand the importance of it, usually just because the school opened this course, and had to come here to get a pass, after, really to need to use, but found the powerless and regret. For example, figure 9-11-4, tragedy is usually the result of this. So even though it's the end of the course, for the individual did not pay attention to this course, some of the students are late, I still want to mend: data structure and algorithm for the programmer's professional life, that is two of the intersection of the circle part of the heart to master it, your programming path will be smooth.
finally send everyone the movie "When Happiness Knocks" in the sentence: "You got a dream,you gotta protect it." People can ' t do something themselves, they wanna tell you can ' t do it. If you are want something, go to it. Period. If you have a dream, you must defend it. When people can't do it, they want to tell you, and neither can you. If you want something, you've got to fight for it. That's the kind of. "
classmates, goodbye. Source: http://www.cnblogs.com/cj723/archive/2011/04/29/2033000.html