(算法设计技巧与分析)QuickSort
2015-03-10 22:16
267 查看
#include<iostream> #include<time.h> #include<Windows.h> using namespace std; int Split(int a[],int low,int high); void QuickSort(int a[],int low,int high); int main() { int a[1100]={4,6,3,1,8,7,2,5}; double start,end; srand((unsigned int(time(NULL))));//导入时间种子加每次运行结样试下伪随机数种子 for (int i=0;i<1100;i++) a[i]=rand(); start=GetTickCount(); QuickSort(a,0,1099); end=GetTickCount(); for(int i=0;i<1100;i++) cout<<a[i]<<" "; cout<<endl; cout<<end-start<<endl; return 0; } int Split(int a[],int low,int high) {//选第一个数为参照数,排序结果a[low]左侧都比a[low]少,a[high]右侧都比a[low]大 int i=low,temp; for(int j=low+1;j<=high;j++) if(a[j]<a[low]) { i++; if(i!=j) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } temp=a[i]; a[i]=a[low]; a[low]=temp; return i; } void QuickSort(int a[],int low,int high) { if(low<high) { int w=Split(a,low,high); QuickSort(a,low,w-1); QuickSort(a,w+1,high); } }
相关文章推荐
- (算法设计技巧与分析)InsertionSort
- (算法设计技巧与分析)Dijkstra
- <算法设计技巧与分析>M.H>Alsuwaiye编著 读书心得
- (算法设计技巧与分析)BottomUpSort
- 算法设计技巧与分析(1)二分搜索的前提——线性搜索
- (算法设计技巧与分析)BubbleSort
- (算法设计技巧与分析)Kruskal
- (算法设计技巧与分析)SelectionSortFec
- (算法设计技巧与分析)RadixSort
- (算法设计技巧与分析)InsertionSort
- (算法设计技巧与分析)MergeSort
- 算法设计技巧与分析
- (算法设计技巧与分析)Exprec
- (算法设计技巧与分析)Permutationsi
- (算法设计技巧与分析)CloseStpair
- 算法设计技巧与分析笔记 第一章
- (算法设计技巧与分析)LinearSearch
- (算法设计技巧与分析)Majority
- (算法设计技巧与分析)BinarySearchrec
- (算法设计技巧与分析)MinMax