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