Fast sorting algorithm, online related articles have been introduced a lot of data structure in the textbook also has a very detailed introduction. In this paper, we need to elaborate a sort of fast sorting algorithm instead of a full sorting algorithm. The so-called partial fast sorting algorithm is to obtain the largest number of sequential records in a sequence by ordering. For example, we need to get the first 100 ordered records from an array of 1 million records, and in order to show the user from large to small, this application in the search engine is often used, very few people have the patience to 1 million of the records are read once, the general reading the first hundreds of records can be satisfied with their answers. In fact, this algorithm is much like the implementation of top N in SQL Server, but the database is the record through the B + Tree index of the way the Organization, the implementation method is completely different. This article needs to elaborate is not through the database, how efficiently completes top n this partial sorting function.
Before we start, let's look at 2 other sorts of sorting methods, if you want to do partial sorting.
1. Select sort
Select the sorting method to achieve partial sorting is very simple, if you need to get the length of M in the array of the first n sorting records, you only need to do the M record n times scan, each scan will find the remaining sequence of the largest (or minimum) can be completed. When N is far less than M, the time complexity of selecting a sort is probably O (m*n).
2. Heap Sorting
The sort can be said to be the most commonly used algorithm to implement partial ordering, including the famous search component Lucene uses heap sorting to achieve partial ordering. Specific algorithm I found one on the Internet, see the partial sorting problem. According to this article, the implementation complexity of heap sort implementation partial ordering is O (MLOGN)
Now let's start with a partial quick sort algorithm.
A quick sort of order in which the array can be divided into two parts based on a datum value, all the numbers before the datum are smaller than the datum, and all the numbers after the datum are larger than the datum. As shown in the figure above, 5 is the base value. The fast sorting algorithm of the whole sort, after realizing a sort of order, then carries on the same sort to the sequence before and after the datum by recursion until all the data are in order.
We can skillfully use this method of datum segmentation to achieve partial ordering. If you want to implement a partial sort, we do not need to sort all the data, in fact, each time only need to do a trip to the base of a sort on it, the principle is very similar to the binary search. As shown above, if we want to get the smallest of the first 2 sorted data, all we need to do is redo the sequence of 5 before the second sort, without having to do the data after 5, since 5 is ranked 6th after a row, and the data after that is certainly not in the top 2 digits. Assuming that the second time we choose a value of 2 in the middle position of the sequence before 5, then the second order is the following procedure
3 4 2 1 5 5 9 8 7
_ 4 3 1 5 5 9 8 7 (2)
1 4 3 _ 5 5 9 8 7 (2)
1 _ 3 4 5 5 9 8 7 (2)
1 2 3 4 5 5 9 8 7