You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Htu1/使用希尔排序进行排序.c

56 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

//使用希尔排列进行升序排列
#include <stdio.h>
#include <malloc.h>
void shellSort(int *a, int len);
int main(void)
{
int i, len, * a;
printf("请输入要排的数的个数:");
scanf("%d",&len);
a = (int *)malloc(len * sizeof(int));
printf("请输入要排的数:\n");
for (i = 0; i < len; i++)
{
scanf("%d",&a[i]);
}
shellSort(a, len);
printf("希尔升序排列后结果为:\n");
for (i = 0; i < len; i++)
{
printf("%d\t",a[i]);
}
printf("\n");
return 0;
}
void shellSort(int *a, int len)
{
int i, j, k, tmp, gap;
for (gap = len / 2; gap > 0; gap /= 2)
{
for (i = 0; i < gap; ++i)
{
for (j = i + gap; j < len; j += gap)
{
tmp = a[j];
k = j - gap;
while (k >= 0 && a[k] > tmp)
{
a[k + gap] = a[k];
k -= gap;
}
a[k + gap] = tmp;
}
}
}
}
/*①时间复杂度
希尔排序的时间复杂度依赖于增量序列的函数有人在大量的实验后得出的结论当n在某个特定的范围后在最优的情况下希尔排序的时间复杂度为O(n1.3)在最差的情况下希尔排序的时间复杂度为O(n2).
空间复杂度
希尔排序的空间复杂度O(1).
②是不稳定的排序算法
Process exited after 7.586 seconds with return value 0*/