Shell sort

Algorithm theory

Cher Sort Name (Shellsort) stems from its inventor Donald Shell, the algorithm is one of the first quadratic time algorithm to break the barrier, however, until several years after it was originally found proof of its sub quadratic time bound. It works by comparing the elements apart a predetermined interval; each trip distance used in the comparison with the algorithm of distance decreases, up until only the last trip comparator adjacent sorting elements. For this reason, Cher sort sometimes called reduced incremental sort (diminishing increment sort)

Graphic algorithm

image.png

template <typename T>
void shellSort(vector<T> & v)
{
	for (int gap = v.size() / 2; gap > 0; gap/=2)
	{
		// 插入排序代码
		for (int i = gap; i < v.size(); i++)
		{
			T tmp = v[i];
			int j = i;
			for (; j >= gap && v[j - gap] > tmp; j -= gap)
			{
				v[j] = v[j - gap];
			}
			v[j] = tmp;
		}
	}
}

time complexity

Preferably: O (n)
worst: O (nlog2n)
Average: O (nlog2n)

Released nine original articles · won praise 1 · views 28

Guess you like

Origin blog.csdn.net/qq_43479736/article/details/105082481