比较型排序算法总结
增量排序(shell 排序):该算法的复杂度要略小于插入排序算法,但是也基本上认为是亚O(N^2)。实现的基本过程如下,选择一个较大的增量gap,一般选择为数组长度的一般作为起始的增量。然后从当前增量作为起始下标开始访问,比较a[i]和a[i-gap],如果a[i] a[0],这是已经处理过的。如果a[i] >
时间复杂度的测试,首先测试有序数组的排序操作,测试代码和算法效果如下所示: #define LEN 100000 int main() srand((int)time(0)); for(i = 0; i < LEN; ++ i) _start = clock(); _start = clock(); _start = clock(); 结果如下: [gong@Gong-Computer sort]$ ./quickSort 从上面的实验结果可知,当为有序数组时,快速排序的速度并不快,甚至是最慢的。插入排序反而是最快的方式。同时我们可以发现采用上面的改进的快速排序算法排序速度很快,并不像传统的算法那么费时间。 [gong@Gong-Computer sort]$ ./quickSort 从上面的结果可知,对于非有序的数组,快速排序的效果是非常好的,但是插入排序就非常的差劲啦。
{
int i = 0;
int a[LEN];
int b[LEN];
int c[LEN];
int d[LEN];
int e[LEN];
clock_t _start, _end;
double times = 0;
{
a[i] = i;
b[i] = a[i];
c[i] = a[i];
d[i] = a[i];
e[i] = a[i];
}
TQuickSort(a,LEN);
_end = clock();
times = (double)(_end - _start)/CLOCKS_PER_SEC;
printf("The QuickSort took %fs",times);
_start = clock();
QuickSort(b,LEN);
_end = clock();
times = (double)(_end - _start)/CLOCKS_PER_SEC;
printf("The Changed QuickSort took %fs",times);
#if 10
_start = clock();
MergeSort(c,LEN);
_end = clock();
times = (double)(_end - _start)/CLOCKS_PER_SEC;
printf("The MergeSort took %fs",times);
InsertionSort(d,LEN);
_end = clock();
times = (double)(_end - _start)/CLOCKS_PER_SEC;
printf("The InsertionSort took %fs",times);
heapSort(e,LEN);
_end = clock();
times = (double)(_end - _start)/CLOCKS_PER_SEC;
printf("The heapSort took %fs",times);
#endif
return 0;
}
The QuickSort took 12.850000s
The Changed QuickSort took 0.000000s
The MergeSort took 0.030000s
The InsertionSort took 0.000000s
The heapSort took 0.020000s
测试采用随机数产生的数组进行排序时的实验效果:
The QuickSort took 0.020000s
The Changed QuickSort took 0.010000s
The MergeSort took 0.030000s
The InsertionSort took 15.240000s
The heapSort took 0.020000s
比较型排序算 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)