Insert sort (top): Insert sort directly, insert sort directly

Source: Internet
Author: User

Insert sort (top): Insert sort directly, insert sort directly

1. Thoughts

Direct sorting can be divided into two parts: ordered and unordered.

From this figure, we can see the idea of directly inserting sorting.

Compare the first part of the unordered part with the ordered part.

Compare the data from the back of the ordered part, and then constantly move the data position of the ordered part.

Static void InsertSort (List <int> list) {for (int I = 1; I <list. count; I ++) {int temp = list [I]; int j = I; while (j> = 1 & temp <list [j-1]) {list [j] = list [j-1]; j --;} list [j] = temp ;}}

 

2. Complexity

In the best case of direct insertion sorting, the time complexity is O (n), and in the worst case, the complexity is O (n2 );

For proof, see:

Insert sorting and Complexity Analysis

 

3. Direct insertion and sorting vs quick 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.