Analysis of Three sort Inserts

Source: Internet
Author: User

Analysis of Three sort Inserts
The basic idea of Insertion Sort is: Insert a record to be sorted to the appropriate position in the subfile that has been sorted in the preceding order according to its keyword size, until all records are inserted.

1. Sort directly inserted

Insert sorting: When the I-th element is inserted, v [0], v [1], v [2] ...... v [I-1], sorted. at this time, the insertion code of v [I] and v [I-1], v [I-2],... sort the codes and find the inserted position, that is, insert v [I]. The elements at the original position are removed from the back to the back.

Template
Void InsertSort (T a [], int n)
{
For (int I = 1; I <n; ++ I)
{
Int tmp = a [I];

Int j;
For (j = I-1; j> = 0 & a [j]> tm p; -- j)
A [j + 1] = a [j];
A [j + 1] = tmp;

}
}

Time Complexity:
From the time analysis, the outer loop needs to be inserted n-1 times. Each insert operation must be at least one time (in the forward direction) and moved twice. At most, the insert operation can be compared to I, And I + 2 times (in the reverse direction) (I = 1, 2 ,..., N-1 ). If Cmin, Cmax, and Cave are used to represent the minimum, maximum, and average values of the total number of comparisons of an element, Mmin, mmax and Mave indicate the minimum, maximum, and average values of the total number of moving elements. The values corresponding to the preceding direct Insertion Algorithm are:
Cmin = n-1 Mmin = 2 (n-1)
Cmax = 1 + 2 +... + N-1 = (n ^ 2-n)/2 Mmax = 3 + 4 +... + N + 1 = (n ^ 2 + 3n-4)/2
Cave = (n ^ 2 + N-2)/4 Mmax = (n ^ 2 + 7n-8)/4
Therefore, the time complexity of directly inserting sorting is O (n ^ 2 ).

From the analysis of time complexity, we can see that when the elements to be sorted are sorted in ascending or descending order, the number of comparisons and movements are less; when the sorted elements are sorted in descending or descending order, the number of comparisons and moves are large, therefore, insertion sorting is more suitable for the basic order (positive order) of raw data.

Although the insertion method is complex as O (n ^ 2) in the worst case, the insertion sorting method is a fast sorting method for small-scale input. Many complex sorting methods, such as quick sorting, are used for sorting when the scale is small.

Spatial complexity:
First, from the perspective of space, it only needs the auxiliary space of an element, which is used to switch the position of the element O (1 ).

Stability:
Insert sorting is stable, because elements with the same value must be inserted after the element with the same value, that is, the relative order remains unchanged.

Applicability:
Insert sorting is a simple sorting method. It not only applies to the sequential storage structure (array), but also applies to the link storage structure. However, when directly inserting and sorting on the Link storage structure, instead of moving the position of the element, modify the corresponding pointer.

Ii. Binary insertion

Binary insert sort: an element sequence v [0], v [1], v [2] in a data table... v [n]. v [0], v [1], v [2] ...... v [I-1] is a sorted element. Insert v [I]. Use a half-fold search to find the insertion position of v [I.

Binary insertion sorting is a stable sorting. When n is large, the total number of sort code comparisons is much better than the worst case of direct insertion sorting, but it is worse than the best case. When the initial sequence of all elements is close to order by sort code, direct insert sorting is less frequently than binary insert sorting. The moving times of binary insertion sorting elements are the same as those of direct insertion sorting, which depends on the initial sequence of elements.

Template
Void BinaryInsertSort (T a [], int n)
{
For (int I = 1; I <n; ++ I)
{
Int left = 0,
Right = I-1;
Int tmp = a [I];
While (left <= right)
{
Int middle = (left + right)/2;
If (a [I] right = middle-1;
Else
Left = middle + 1;
}
Int j;
For (j = I-1; j> = left; -- j)
A [j + 1] = a [j];
A [j + 1] = tmp;
}
}

Iii. Hill sorting

Basic Idea: First take an integer d1 less than n as the first increment, and divide all records of the file into d1 groups. All records whose distance is a multiple of dl are placed in the same group. Insert and sort directly in each group. Then, take the second incremental d2.

Incremental sequence selection:

The execution time of Shell sorting depends on the incremental sequence. Common Features of a good incremental sequence: ① The last increment must be 1; ② try to avoid the mutual multiples of values in the sequence (especially adjacent values. A large number of experiments have been conducted to show the current good results: WHEN n is large, the number of comparisons and moves is between n and 1.6n. Reference code: template Void ShellSort (T a [], int n) {for (int gap = n/2; gap> = 1; gap/= 2) {for (int I = gap; I <n; ++ I) {int tmp = a [I]; int j; for (j = I-gap; j> = 0 & a [j]> tmp; j-= gap) a [j + gap] = a [j]; a [j + gap] = tmp ;}}}

Performance analysis:

The reason why the time performance of hill sorting is better than that of direct insertion sorting:
① When the initial state of the file is basically ordered, the number of comparisons and moves required for direct insertion sorting is relatively small.
② When the n value is small, the difference between n and n2 is also small, that is, the best time complexity of direct insertion sorting O (n) and the worst time complexity 0 (n2) are not much different.
③ At the beginning of the hill sorting, there were a large increase in the number of groups and a small number of records in each group. Therefore, the number of records in each group was rapidly inserted. Later, the incremental di gradually reduced and the number of groups gradually reduced, the number of records in each group gradually increased, but because the di-1 has been used as the distance sorting order, the file is closer to the orderly state, so the new sorting process is also faster.
Therefore, the efficiency of hill sorting is much higher than that of direct insertion sorting.

Stability:

Hill sorting is unstable.



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.