考研数据结构与算法之折半插入排序

C语言代码:

void InsertSort_2(int A[], int n)
{
	int i,j,low,high,mid,temp;
	for(i = 1; i < n; i++)
	{
		temp = A[i];
		low = 0;
		high = i-1;
		while(low <= high)
		{
			mid = (high+low)/2;
			if(A[mid] < temp)
				low = mid + 1;
			else 
				high = mid - 1;
		}
		for(j = i-1; j >= high+1; --j)
		{
			A[j+1] = A[j];
		}
		A[high+1] = temp;
	}
} 

时间复杂度为O(n^2),原理上和直接插入排序一样,期间加入了折半查找;

你可能感兴趣的:(积累)