插入排序_折半插入排序
程序代码:
void BInsertionSort(Elem R[],int n)
{
for(i = 2;i <= n;i++)
if(R[0] < R[i])
{
R[0] = R[i];
low = 1;
high = i-1;
while(low <= high)
{
mid = (low+high)/2;
if(R[0] < R[mid])
high = mid-1;
else
low = mid+1;
}
for(j = i-1;j >= high + 1;j--)
R[j+1] = R[j];
R[j+1] = R[0];
}
}
时间复杂度:比较次数评价为O(nlogn),移动次数与直接插入相比没有减少,总复杂度仍为O(n2)。
稳定性:稳定。