Direct Insert Sort

Source: Internet
Author: User

1. Algorithm Introduction

Each time the first element is taken out of the unordered table, it is inserted into the proper position of the ordered table so that the ordered table remains orderly.

2. Algorithm principle

First pass compares the first two numbers, then inserts the second number by the size into the ordered table;

The second one is to scan the third data with the first two numbers, and insert the third number into the ordered table by size;

Proceed sequentially, the entire sequencing process is completed after the (n-1) scan.

3. Source code

C:

1 voidInsertsort (int[] Array) {2          for(intI=1; i<array.length;i++)3         {4             if(array[i]<array[i-1])5             {6                 inttemp=Array[i];7                 intK = i-1;8                  for(intj=k;j>=0&& temp<array[j];j--)9                 {Tenarray[j+1]=Array[j]; Onek--; A                 } -array[k+1]=temp;//Insert the value of the I-bit -             } the         }  -}

4. Complexity of Time:

O (n^2)

5. Stability analysis

It's stable.

Direct Insert Sort

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.