最近在复习排序和查找算法的时候,回想算法课程和数据结构课程上面各种写法,总结一下,顺便比较了一下它们之间的效率,
另《外数据结构》书本上阐述,如果比较的枢纽值不是第一个或者最后一个而是 a[low] ,a[high],a[(high+low)/2] 的中间值,效率还会好很多,但是我在实现的过程中,总是搬移到了错误的位置,没有实现
#include <iostream.h> #include <windows.h> #include <ctime> #include <math.h> #include <cstdlib> #include <stdio.h> void QuickSort( int a[],int low,int high); //比较经典的一种,将往中间扫面时找到的满足条件的交换 void QuickSort3( int a[],int low,int high); //枢纽暂存,每次找到一个比枢纽大或者小的,就放到上一次搬离的位置,最后把枢纽放回到low处(低地址必须小于高地址,不能等于) void QuickSort2( int a[],int low,int high); //只有一个while,指针都从头部开始,,快指针每次都向后移动,遇到一个比枢纽大的就和慢指针交换值 //产生随机数 int randArr(int * pint , int size); int size = 0; int main() { int a[] ={4,-2,3,19,0,-4,99,7,2,-5,0,-11,2,2,56,-8,0,17,200,5,1,3,5,4,6,-5,29,-1,8}; int b[] ={ 34,51,38,65,119,76,16,27}; //int b[] ={76, 119 }; int tsize=150000; int *pint = new int[tsize]; int *pint2 = new int[tsize]; int *pint3 = new int[tsize]; int id = 5; if(! randArr(pint,tsize) ) return 0; // memcpy(pint ,a,sizeof(int) * tsize); memcpy(pint2,pint,sizeof(int) * tsize); memcpy(pint3,pint,sizeof(int) * tsize); size = tsize; printf("=====before====\n"); for(id = 0 ; id< 10;id++) { printf("%3d ", pint[id]); }printf("=====before====\n"); int start = GetTickCount(); QuickSort(pint,0,size -1); cout<<"time QuickSort used="<< GetTickCount() - start << endl; for(id = 0 ; id< 10;id++) { printf("%3d ", pint[id]); }printf("======QuickSort===\n\n"); start = GetTickCount(); QuickSort2(pint2,0,size -1); cout<<"time QuickSort2 used="<< GetTickCount() - start << endl; for(id = 0 ; id< 10;id++) { printf("%3d ", pint2[id]); }printf("======QuickSort2===\n\n"); QuickSort3(pint3,0,size -1); cout<<"time QuickSort3 used="<< GetTickCount() - start << endl; for(id = 0 ; id< tsize;id++) { if(pint[id] != pint2[id]) { printf("Confliction!! %d",id); break; } }printf("======QuickSort3===\n\n"); return 0; } void QuickSort(int a[],int l,int h) { int po; int high = h , low = l; if(low < high ) { po = a[l]; low++; while(1) { while(low <= high && a[high] >= po) high--; while(low <= high && a[low] <= po) low++; if(low < high) { a[low] ^= a[high]; a[high] ^= a[low]; a[low] ^= a[high]; low++; high--; } else break; } a[l] = a[high]; a[high] = po; QuickSort(a,l,high-1); QuickSort(a,high+1,h); } } void QuickSort2(int a[],int l ,int h) { int po; int high = h,low = l; if( l < h ) { po = a[l]; while( low < high) { while( low < high && a[high] >= po ) high--; a[low] = a[high]; while( low < high && a[low] <= po ) low++; a[high] = a[low]; } a[low] = po; QuickSort2(a,l,low-1); QuickSort2(a,low+1,h); } } void QuickSort3(int a[],int l ,int h ) { int high = l+1, low = l+1; int po = a[l]; if( l < h) { while( high <= h) { if( a[high] < po) //找到慢指针 { if(high != low) { a[low] ^=a[high]; a[high] ^=a[low]; a[low] ^=a[high]; } low++; } high++; } if(low-1 != l) { a[low-1] ^=a[l]; a[l] ^=a[low-1]; a[low-1] ^=a[l]; } low--; QuickSort3(a,l,low - 1); QuickSort3(a,low+1 ,h); } } int randArr(int * pint , int size) { int i = 0; if(!pint) return 0; srand((unsigned int)time(NULL)); for( i = 0 ; i<size; i++) { pint[i] = rand() % 100 ; if( rand() % 10 == 1 && rand() % 10 == 1 && rand() % 10 == 1 &&pint[i] % 10 == 2) pint[i] *= -1; } return 1; }