- 插入排序
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;
}
}
}
时间: 2024-12-22 14:30:52