内排序之shell排序

内排序之shell排序

shell排序的时间复杂度为O(n logn )

template<typename E> bool prior(E a, E b)//判断两个数的大小
{
	if (a < b)
		return true;
	else return false;
}

template<typename E> inline void swap(E A[], int i, int j)//交换两个位置上的数据
{
	E temp = A[i];
	A[i] = A[j];
	A[j] = temp;
}
template<typename E> void inssot2(E A[], int n, int incr)//shell排序中使用插入排序方法
{
	for (int i = incr; i < n; i += incr)
		for (int j = i; (j > incr) && prior(A[j], A[j - incr]); j -= incr)
			swap(A, j, j - incr);
}

template<typename E> void shellsort(E A[], int n)//shell排序
{
	for (int i = n / 2; i > 2; i /= 2)
		for (int j = 0; j < i; j++)
			inssot2<E>(&A[j], n - j, i);
	inssot2<E>(A, n, 1);
}

猜你喜欢

转载自blog.csdn.net/qq_43667986/article/details/83961762