Sorting Algorithm-preknowledge_ sorting algorithm

Source: Internet
Author: User

Notes as a "liar data structure"

One, what is the sort

Suppose that the sequence {r1,r2,..., Rn} containing n records has the corresponding keyword {k1,k2,..., kn}, it is necessary to determine an arrangement of 1,2,...,n P1,P2,..., PN so that its corresponding keyword satisfies kp1<=kp2< .... <=KPN (or non-ascending) relationships, even if the sequence becomes a sequence of keywords ordered {rp1,rp2,..., RPN}, such operations are called sorting.

EX:ABCD's achievements are as follows

A 75

B 98

C 63

D 87

Sorted by grade from big to small results for BDAC
Second, the stability of the order

Suppose Ki=kj (1<=i<=n,1<=j<=n,i!=j), and in the sequence before the sort, RI is ahead of RJ (that is, i<j). If the RI is still ahead of RJ after sorting, the ordering method used is stable, and conversely, if it is possible to make the sorted sequence of RJ-leading RI, the ordering method used is not stable.

The original arrangement of EX:ABCD is as follows

A 75

B 75

C 87

D 96

Stable sort result Dcab (the relative position of AB does not change)

Unstable sort result DCBA (relative position of AB changed)

Three, inner sort and outside sort

Depending on whether the records to be sorted in the sort process are all placed in memory, the sort is divided into: inner sort and outer sort

The inner sort is in the whole process of sorting, all the records to be sorted are placed in memory.

Sorting is because there are too many sort records to be placed in memory at the same time, and the whole sort process needs to be exchanged between inside and outside for multiple data.

The algorithm performance of the internal sorting (the algorithms described here are all internal sorting)

1. Complexity of Time

In the inner sort, there are two main types of operations: comparison and movement. Comparisons refer to the comparison between keywords, which is the minimum operation to do a sort. Move refers to a record moving from one location to another. Efficient internal sorting algorithms should have as few keyword comparisons and as few record movements as possible.

2. Space complexity

In addition to storing the storage space to be sorted, perform the additional storage space required by the algorithm.

V. Classification

According to the complexity of the algorithm is divided into two major categories

Simple algorithm: Bubble sort, simple select sort, direct insert sort

Improved algorithm: Hill sort, heap sort, merge sort, quick sort

The purpose of learning these sorting algorithms is not so much to be programmed in reality, but to improve the ability of our programming algorithms to solve more complex and flexible application problems.

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: 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.