Algorithm Note: insert sorting

Source: Internet
Author: User

I. Thoughts

Insert sort: first, compare the size of the 2nd elements with the elements of the child array, starting from one element sub-array element, find a suitable location (and move the corresponding child array elements behind) and insert it. Then, compare the size of the 3rd elements with the child array (two elements) in sequence, insert it to the response position. In this way, the histogram sorts the entire array;

Ii. Comparison with selected sorting

  • Same: the elements on the left of the current index are ordered, but the final position of the inserted index is still unknown. In order to make room for smaller elements, it may move;

  • Different: the time required for inserting sorting depends on the initial order of the elements in the input (for large and array elements already ordered (or near-ordered) the sorting of arrays is much faster than that of arrays in random or reverse order;

    Iii. Code

    /*** Insert sorting ** @ author pengcx **/public class Insertion extends Sort {public static void main (String [] args) {String [] a = {"d", "a", "w", "B", "q"}; Insertion. sort (a); show (a);}/*** sort array a ** @ param a * sort array a */private static void sort (Comparable []) {int N =. length; for (int I = 0; I <N; I ++) {for (int j = I; j> 0 & less (a [j], a [j-1]); j --) {exch (a, j, j-1 );}}}}


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.