Data structure sorting

Source: Internet
Author: User

The relationship of values that correspond to different orders of magnitude:

O (1) (the number of basic operations is independent of the problem scale N, also known as the constant order) <o (log2 ^n) <o (n) <o (n log 2^n) <o (n ^2) <o (n ^3) <o (2^n) <o (n!)

1. Select sort: Unstable, time complexity O (n^2)

Basic idea: Treat the sequencing sequence of records to be processed n-1, and the first I-pass processing is to be l[i. N] The smallest person in the l[i] exchange position. Thus, the position of the first I record is already correct after I pass the processing

2. Insertion sort: Stable, time complexity O (n^2)


The basic idea of inserting a sort is that, after i-1-through, l[1..i-1] is in the right order.
I-pass processing only l[i] into the appropriate position of l[1..i-1], so that l[1..i] is a sequence of orderly.
To achieve this, we can use a sequential comparison method. First compare L[i] and l[i-1],
If L[i-1]≤l[i], then L[1..I] has been sequenced, and the first time processing is over;
Otherwise swap l[i] with l[i-1], continue to compare l[i-1] and l[i-2] until a location of J (1≤j≤i-1) is found,
Make l[j]≤l[j+1]. Figure 1 illustrates the process of inserting a sequence of 4 elements, which requires (a), (b), (c) three insertions.


3. Bubble sort: Stable, time complexity O (n^2)
The bubble sort method is the simplest sort method. The basic idea of this approach is to think of the elements to be sorted as "bubbles" arranged vertically,
The smaller elements are lighter, so they move upward. In the bubble sorting algorithm we have to deal with this "bubble" sequence several times.
The so-called process, is to check the sequence from the bottom up, and always pay attention to the sequence of two adjacent elements is correct.
If the order of two adjacent elements is found to be incorrect, that is, the "light" elements are below, exchanging their positions.
Obviously, after processing, the "lightest" element floats to its highest position, and after two times, the "light" element floats to the next high position.
In the second pass, you do not have to check because the element at the highest position is already the lightest element.
In general, when I pass the processing, I do not have to check the higher position above the elements, because after the previous i-1 the processing, they have been correctly sequenced.


4. Heap sequencing: Unstable, time complexity O (nlog N)
Heap sorting is a sort of tree selection, in which the a[n] is considered as the sequential storage structure of a complete binary tree.
Select the smallest element by using the intrinsic relationship between the parent node and the child's node in the complete binary tree.


5. Merge sort: Stable, time complexity O (nlog N)
has two sequential (ascending) sequences stored in adjacent positions in the same array,
Set as A[l. M],a[m+1..h], merge them into an ordered sequence, and store them in A[l. H].


6. Quick sort: Unstable, time complexity optimal O (Nlogn) worst time O (n^2)

A quick sort is an essential improvement to the bubbling sort.
Its basic idea is that the length of the sequencing sequence can be drastically reduced after a scan.
In a bubbling sort, a scan can only ensure that the number of maximum values is moved to the correct position, while the length of the sequence to be sorted may be reduced by only 1.
Quick sort by a scan, you can make sure that the number of the left is smaller and the number on the right is larger than it.
It then uses the same method to manipulate the left and right sides of the number until there is only one element to the left of the datum point.


7. Hill sort: Unstable, time complexity average time O (nlogn) worst time O (n^s) 1<s<2
In the direct insertion sorting algorithm, inserting one number at a time causes the ordered sequence to increment by only 1 nodes,
And does not provide any help in inserting the next number. If you compare the number of distances (called increments) that are far apart,
When a number moves across multiple elements, a comparison can eliminate multiple element exchanges.
D.l.shell realized this idea in 1959 in a sort algorithm named after him.
The algorithm first sorts the set of numbers by an increment d into groups, each group of records of the subscript difference D. Sort all the elements in each group,
It is then carried out in a smaller increment and then sorted in each group. When the increment is reduced to 1 o'clock,
The entire number to be sorted is divided into a group, and the sort is completed.

Data structure sorting

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.