4-1 Sorting and Searching

Sorting and Searching

Sorting algorithm (English: Sorting algorithm) is a series of data can be arranged in accordance with an algorithm specific order.

Stability sorting algorithm

Stability: stable sorting algorithms originally had equal keys will maintain the relative order of record. That is, if a sorting algorithm is stable if there are two equal keys record R and S, and R appears before S in the original list, R in the sorted list and it will be before S.

When equal elements are indistinguishable, such as for example an integer, the stability is not a problem. However, assuming that the number of the following will be at their first digital sort.

(4, 1)  (3, 1)  (3, 7)(5, 6)

At this stage, it is possible to produce two different results, with equivalent key is to make a record to maintain relative order, and the other is not:

(3, 1)  (3, 7)  (4, 1)  (5, 6)  (维持次序)
(3, 7)  (3, 1)  (4, 1)  (5, 6)  (次序被改变)

Unstable sorting algorithms may change the relative order of records in the equivalent key in, but stable sorting algorithms never the case. Unstable sorting algorithms can be specially implemented to stabilize. A way for this matter is more artificial extension keys, the comparison between two such objects in other respects the same key, (such as adding a second comparison the above criteria: the size of the second key-values) it will be decided to use the entries in the original data in order, as a tiebreaker. However, keep in mind that this order usually involves extra space burden.

Guess you like

Origin www.cnblogs.com/echo-kid-coding/p/11363248.html