快速排序的优化及其与qsort的比较
2011-05-09 19:02
344 查看
快速排序是用的最多的一种排序方法,《编程珠玑》在第十一章《排序》中讨论了快速排序的几个版本,其实版本的不同,都是体现在partition函数上。
partition版本1,其实就是《算法导论》给出的版本,很好理解;
partition版本2:其实本科的时候最先学习快速排序,就是使用的这个版本。后来看到版本1,觉得版本1更好理解。但是该版本的效率更高,使用了两个指针:
partition 版本3:对版本2的优化:其实不用每次交换a[low]和a[high],直接覆盖好了,类似于前面的插入排序的改进
测试程序及其结果
结果:
版本1:the cost time is 141
版本2:the cost time is 125
版本3:the cost time is 110
qsort: the cost time is 94
partition版本1,其实就是《算法导论》给出的版本,很好理解;
int partition1(int a[], int begin, int end) { int pivotkey = a[end]; int i = begin; int j = begin - 1; int tmp; for (i = begin; i < end; i ++) { if (a[i] <= pivotkey) { j ++; tmp = a[j]; a[j] = a[i]; a[i] = tmp; } } j ++; tmp = a[j]; a[j] = a[end]; a[end] = tmp; return j; }
partition版本2:其实本科的时候最先学习快速排序,就是使用的这个版本。后来看到版本1,觉得版本1更好理解。但是该版本的效率更高,使用了两个指针:
int partition2(int a[],int begin, int end) { int pivotkey = a[begin]; int tmp; int low = begin; int high = end; while (low < high) { while (low < high && a[high] >= pivotkey ) --high; tmp = a[low]; a[low] = a[high]; a[high] = tmp; while(low < high && a[low] <= pivotkey) ++low; tmp = a[low]; a[low] = a[high]; a[high] = tmp; } return low; }
partition 版本3:对版本2的优化:其实不用每次交换a[low]和a[high],直接覆盖好了,类似于前面的插入排序的改进
int partition3(int a[],int begin, int end) { int pivotkey = a[begin]; int low = begin; int high = end; while (low < high) { while (low < high && a[high] >= pivotkey ) --high; a[low] = a[high]; while(low < high && a[low] <= pivotkey) ++low; a[high] = a[low]; } a[low] = pivotkey; return low; }
测试程序及其结果
# include <stdio.h> # include <stdlib.h> # include <time.h> # include "chapter11.h" # include "chapter12.h" # define N 500000 int cmp(const void* a,const void* b) { return *(int *)a - *(int *)(b); } int main() { // testchapter12 // int m = 10, n = 100; // genknuth(m, n); // genbyme(m,n); // int a[] = {5,7,6,4,2,8,6}; // int a[] = {6,4,10,12,7,8}; int a ; int i; for(i = 0; i < N; i ++) a[i] = rand()%(10 * N); int len = sizeof (a)/ sizeof(a[0]); clock_t start = clock(); // quickSort1(a, 0, len - 1); // quickSort2(a, 0, len - 1); // quickSort3(a, 0, len - 1); qsort(a,len,sizeof(a[0]),cmp); // partition3(a,0,len - 1); clock_t end = clock(); printf("the cost time is %ld", (long int)end - start); return 0; }
结果:
版本1:the cost time is 141
版本2:the cost time is 125
版本3:the cost time is 110
qsort: the cost time is 94
相关文章推荐
- 快速排序、基准元的选取及其优化
- 排序算法—比较排序之插入排序及其优化
- 6种排序算法及其比较 简单选择排序,堆排序,简单插入排序,希尔排序,冒泡排序,快速排序,归并排序
- 《大话数据结构》读书笔记之快速排序源码及其优化
- 快速排序的几种方法及其优化
- 快速排序及优化
- 优化 快速排序
- 快速排序的三种优化
- 【模版】快速排序,二分查找及其求其上下界函数
- 插入排序及其优化
- 十二.C语言8种排序算法及其实现 1.希尔排序 2.二分插入法 3.直接插入法 4.带哨兵的直接排序法 5.冒泡排序 6.选择排序 7.快速排序 8.堆排序
- 快速排序的三种写法的效率比较
- 快速排序的算法优化要点
- 快速排序的C语言实现及其时间复杂度
- Java排序算法优化--快速排序【温故而知新】
- 快速排序的库函数qsort与sort的使用方法
- 冒泡和Arrays类提供的快速排序比较时间
- 插入排序、冒泡排序、选择排序、快速排序、堆排序、归并排序算法比较
- 经典排序之快排及其优化
- qsort快速排序总结