Direct Insert Sort

Source: Internet
Author: User

void Insertsort ()

{

int a[10]={10,2,3,5,8,9,7,1,55,6};

int nval=0;

for (int i=1; i<10; i++)

{

Nval=a[i];

int j=i-1;

while (j>=0 && nval<a[j])

{

A[J+1]=A[J];

j--;

}

A[j+1]=nval;

}

}

#如果待排序列中记录按关键字非递减有序时, the number of times a keyword is required to be compared to the minimum n-1.

#若文件的初始状态是正序的, a scan will complete the sorting. The required number of keyword comparisons and record moves has reached the minimum value:. #所以, the best time complexity for a direct insert sort is.

#时间复杂度为O (N2);

#稳定排序方法

2. Binary Insert sort (keyword ordered)

void Insertbsort ()

{

int a[10]={10,2,3,5,8,9,7,1,55,6};

int nval=0;

for (int i=1; i<10; i++)

{

Nval=a[i];

int low=0;

int high=i-1;

int mid=0;

The position of low is the position of the element to be placed, and low is always greater than the high one scale

while (Low<=high)

{

Mid= (Low+high)/2;

if (Nval<a[mid]) high=m-1;

else low=m+1;

}

int j=i-1;

while (J>=low && nval<a[j])//or while (j>=high+1&& Nval<a[j])

{

A[J+1]=A[J];

j--;

}

A[low]=nval;

}

}

#时间复杂度为O (N2);

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.