|
|
@ -1,4 +1,4 @@
|
|
|
|
//ʹ<EFBFBD><EFBFBD>ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
//使用希尔排列进行升序排列
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
void shellSort(int *a, int len);
|
|
|
|
void shellSort(int *a, int len);
|
|
|
@ -6,16 +6,16 @@ void shellSort(int *a, int len);
|
|
|
|
int main(void)
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int i, len, * a;
|
|
|
|
int i, len, * a;
|
|
|
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ŵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
|
|
|
printf("请输入要排的数的个数:");
|
|
|
|
scanf("%d",&len);
|
|
|
|
scanf("%d",&len);
|
|
|
|
a = (int *)malloc(len * sizeof(int));
|
|
|
|
a = (int *)malloc(len * sizeof(int));
|
|
|
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ŵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
|
|
|
|
printf("请输入要排的数:\n");
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
scanf("%d",&a[i]);
|
|
|
|
scanf("%d",&a[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
shellSort(a, len);
|
|
|
|
shellSort(a, len);
|
|
|
|
printf("ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>к<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>\n");
|
|
|
|
printf("希尔升序排列后结果为:\n");
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
printf("%d\t",a[i]);
|
|
|
|
printf("%d\t",a[i]);
|
|
|
@ -47,9 +47,9 @@ void shellSort(int *a, int len)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*<EFBFBD><EFBFBD>ʱ<EFBFBD>临<EFBFBD>Ӷ<EFBFBD>
|
|
|
|
/*①时间复杂度
|
|
|
|
ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>临<EFBFBD>Ӷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еĺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD>ó<EFBFBD><EFBFBD>Ľ<EFBFBD><EFBFBD>ۣ<EFBFBD><EFBFBD><EFBFBD>n<EFBFBD><EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD>ķ<EFBFBD>Χ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ŵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>临<EFBFBD>Ӷ<EFBFBD>ΪO(n1.3)<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>临<EFBFBD>Ӷ<EFBFBD>Ϊ<EFBFBD><EFBFBD>O(n2).
|
|
|
|
希尔排序的时间复杂度依赖于增量序列的函数,有人在大量的实验后得出的结论:当n在某个特定的范围后,在最优的情况下,希尔排序的时间复杂度为O(n1.3),在最差的情况下,希尔排序的时间复杂度为:O(n2).
|
|
|
|
<EFBFBD>ռ临<EFBFBD>Ӷ<EFBFBD>
|
|
|
|
空间复杂度
|
|
|
|
ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀռ临<EFBFBD>Ӷȣ<EFBFBD>O(1).
|
|
|
|
希尔排序的空间复杂度:O(1).
|
|
|
|
<EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD><EFBFBD>ȶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>㷨
|
|
|
|
②是不稳定的排序算法
|
|
|
|
Process exited after 7.586 seconds with return value 0*/
|
|
|
|
Process exited after 7.586 seconds with return value 0*/
|
|
|
|