Programmer must know 8 big sort 3 Large lookup (2)

Source: Internet
Author: User

in this paper, we first finish the remaining three, and then discuss the stability of the order, and finally summed up the scheduling of time complexity and space complexity.

(1 to 5 see the previous article)

6. Quick Sort

(1) Basic idea: Select a datum element, usually select the first element or the last element, through a scan, the backlog sequence is divided into two parts, one is smaller than the datum element, and the other is greater than or equal to the datum element, when the datum element is in its correct position, And then recursively sort the two parts of the partition in the same way.

(2) Example:



in the diagram above, the sequence of rows is divided into two parts , part smaller than the Datum element , one part is greater than the Datum element , then the process of solving the above two parts is repeated. (This is just a quick sort of implementation, personally considered easier to understand)


7. Merge sort


(1) Basic sort: Merge (merge) sorting method is to combine two (or more than two) ordered table into a new ordered table, that is, to divide the sequence into several subsequence, each subsequence is ordered. Then the ordered Subsequence is merged into the whole ordered sequence.

(2) Example:



8, Cardinal order


(1) The basic idea: all the values to be compared (positive integer) unified to the same number of digits, the number of short digits before the number 0. Then, start at the lowest bit, and then sort it one at a time. This is done from the lowest bit to the highest order, and the sequence becomes an ordered series.

(2) Example:



Stability Note: Before sorting, 2 (or more) equal numbers are placed before and after the sequence in the order in which they are placed in the sequence.


Instance:

Pending sequence: 5,4,8, 6,1,8, 7,9

Sort Result: 1,4,5,6,7,8,8,9

Stability: 1,4,5,6,7,8,8, 9

Instability: 1,4,5,6,7,8,8, 9


Description: Compare red 8 and purple 8 to see where they are sorted before and after. Before the sort, red 8 in the Purple 8 before, if the sort of red 8 still in front of Violet 8, then the sorting algorithm is stable, otherwise unstable.


Now we analyze the stability of the 8 sorting algorithms.

(Please use the basic idea of sorting to understand the stability of the order (8 sorts of basic ideas have been said before, here is no longer to repeat) may be a little fuzzy.

 

(1) Direct insertion Sort : The general insertion sort, compared to the last element of an ordered sequence, is inserted directly behind it if it is larger, otherwise it is always forward. If one is found to be equal to the insertion element, it is inserted behind the equality element. The insertion sort is stable.

(2) Hill sort: Hill sort is to insert the elements according to different step size, once the insertion sort is stable, will not change the relative order of the same elements, but in different insert sort process, the same elements may move in their respective insertion sort, stability will be destroyed, So hill sort is unstable.

(3) Simple selection sort : In a trip, if the current element is smaller than an element, and the small element appears behind an element that is equal to the current element, then the stability of the exchange is corrupted. Light said may be a little fuzzy, to see a small example: 858410, the first scan, the 1th element 8 and 4 Exchange, then the original sequence 2 8 of the relative order and the original sequence inconsistent, so the selection of the order is not stable.

(4) Heap sorting : The process of heap sequencing is to select the maximum (large top heap) or the smallest (small top heap) with a total of 3 values from the beginning of N/2 and its child nodes, and the choice between these 3 elements certainly does not break the stability. But when for n/2-1, N/2-2, ... When these parent nodes select elements, it is possible that the N/2 parent exchange swaps the subsequent element, while the n/2-1 parent node does not exchange the same element after it, so the heap sort is not stable.

 

(5) Bubble sort : It is known from the previous content that the bubble sort is the adjacent two element comparison, the exchange also occurs between these two elements, if two elements are equal, do not exchange. So the bubble sort stabilizes.

(6) Quick sort: in the central element and in the sequence of the exchange of elements, it is very likely to the previous elements of the stability of the upset. Or look at a small example: 6 4 4 5 4 7 8 9, the first order, the central element 6 and the third 4 exchange will destroy the original sequence of element 4, so the fast ordering is unstable.

(7 Merge Sort: when there are 1 or 2 elements in the decomposed child column, 1 elements are not exchanged, and 2 elements are not exchanged if they are equal in size. In the process of sequence merging, if two current elements are equal, we keep the elements in the preceding sequence in front of the result sequence, so the merge sort is also stable.

(8) Cardinal Order: It is sorted by low order, then collected, sorted by high order, then collected, and so on until the highest bit. Sometimes some attributes are in order of precedence, first by low priority, and then by high priority, the final order is high priority in front, high priority of the same low priority high in the front. Cardinality sorting is based on separate sorting, collected separately, so it is stable.

8 kinds of sorting, stability, time complexity and space complexity summary:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.