Algorithm-2. Insert sorting, algorithm insert sorting

Source: Internet
Author: User

Algorithm-2. Insert sorting, algorithm insert sorting

1. Basic Ideas

Usually, the way people organize bridges is one by one, and each card is inserted into a proper position in another sorted card. In computer implementation, in order to make room for the elements to be inserted, we need to move all the other elements one to the right before insertion. This algorithm is called insert sorting.

As with sorting, all the elements on the left of the current index are ordered, but their final position is unknown. To make room for smaller elements, they may be moved. However, when the index reaches the right of the array, the array sorting is complete.

The time required for insertion sorting depends on the initial sequence of elements in the input. For example, sorting a large array whose elements are ordered (or near-ordered) is much faster than sorting an array in random or backward order.

2. Specific Algorithms

/*** Insert and sort ** @ author huazhou **/public class Insertion extends Model {// sort a [] in ascending order public void sort (Comparable []) {int N =. length; // insert a [I] into a [I-1], a [I-2], a [I-3]... for (int I = 1; I <N; I ++) {for (int j = I; j> 0 & less (a [j], a [J-1]); j --) {exch (a, j, J-1 );}}}}

For each I between 0 and N-1, all elements of a [I] And a [0] to a [I-1] smaller than it are exchanged sequentially. When index I changes from left to right, the elements on the left are always ordered, so when I reaches the right of the array, the sorting is complete.

3. Algorithm Analysis

Proposition:For an array with a random length of N and NO duplicate primary keys, it is required to insert the sorting on average ~ N2/4 comparisons and ~ N2/4 exchanges. In the worst case ~ N2/2 comparisons and ~ N2/2 times of exchange, it is best to need the N-1 times comparison and 0 times of exchange.

Proof:Using an NxN trajectory table, you can easily obtain the number of exchanges and comparisons. In the worst case, all elements under the diagonal line need to move their positions. In the best case, they are not needed. For randomly arranged arrays, on average, each element may move the length of half an array backward. Therefore, the total number of exchanges is 1/2 of the total number of elements under the diagonal line.

The total number of comparisons is the number of exchanges plus an additional item. The value N minus the inserted element is the minimum number of known elements. In the worst case (Reverse Order array), this item is negligible relative to the total; in the best case (the array is already ordered), this item is equal to the N-1.

4. Summary

The more general case we want to consider is a partially ordered array. Inversion refers to two elements in the array that are reversed sequentially. For EXAMPLE, there are 11 pairs in the EXAMPLE: e-A, X-A, X-M, X-P, X-L, X-E, M-L, M-E, P-L, P-E and L- e. If the number of inverted arrays is less than a multiple of the array size, we say that the array is partially ordered. Below are several typical Partially Ordered arrays:

• Each element in the array is not far from its final position;

• An ordered large number group is connected to a small array;

• The position of only a few elements in the array is incorrect.

Inserting sorting is very effective for such arrays, but selecting sorting is not. In fact, when there are few inverted values, insertion sorting is likely to be faster than any other algorithm in this chapter. In general, insertion sorting is very efficient for partial ordered arrays and suitable for small-scale arrays. This is important because these types of arrays often appear in practical applications and they are also intermediate processes of advanced sorting algorithms.

Proposition:The number of interchange operations required for insertion and sorting is the same as the number of inverted operations in the array. The number of comparisons required is greater than or equal to the inverted quantity, and the size of the array is reduced by one.

Proof:Each switch changes the positions of the two elements in the reverse order, which is equivalent to reducing a pair of inversion. When the inverted number is 0, the sorting is complete. Each exchange corresponds to a comparison, and each I between 1 and N-1 may require an additional comparison (when a [I] does not reach the left end of the array ).

 

【Source code download]

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.