分类: C++ C语言 数据结构 2012-09-02 20:26 459人阅读
最近在复习排序和查找算法的时候,回想算法课程和数据结构课程上面各种写法,总结一下,顺便比较了一下它们之间的效率,
另《外数据结构》书本上阐述,如果比较的枢纽值不是第一个或者最后一个而是 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);
- void QuickSort2( int a[],int low,int high);
-
- 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 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(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;
- }
快速排序的三种写法的效率比较
原文:http://www.cnblogs.com/bb3q/p/4498746.html