快速排序的三種寫法的效率比較

            最近在複習排序和查找算法的時候,回想算法課程和數據結構課程上面各種寫法,總結一下,順便比較了一下它們之間的效率,

 

另《外數據結構》書本上闡述,如果比較的樞紐值不是第一個或者最後一個而是 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;
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章