插入排序
1.直接插入排序
void InsertSort(int *a, size_t size)//直接插入排序
{
assert(a);
for (size_t i = 0; i < size - 1; ++i)
{
int end = i;
int tmp = a[end + 1];
while (end >= 0 && a[end]>tmp)
{
a[end + 1] = a[end];
--end;
}
a[end+1] = tmp;
}
}
2.希尔排序
//希尔排序
void ShellSort(int *a, size_t size)
{
assert(a);
int gap = size;
while (gap > 1)
{
gap = gap / 3 + 1;
for (size_t i = 0; i < (size - gap); i++)
{
int end = i;
int tmp = a[end + gap];
while (end >= 0 && a[end]>tmp)
{
a[end + 1] = a[end];
end = end - gap;
}
a[end + gap] = tmp;
}
}
}
本文出自 “学习记录” 博客,转载请与作者联系!
原文地址:http://10794428.blog.51cto.com/10784428/1782627