Implementation of QuickSort

Bad luck , I caugh a cold for unknown reason ! My throat was like on fire . It felt like shit ......The last Algorithm

class of this week was in the morning . About 30 students were late for class for at least 5 minutes ! At least 60

students waited for 5 hours totally for them ! Amazing ha ? All right ,forget about that crap ! Let's get straight to

what I did today .Actually , my days were almost the same as yesterday ...... Went to classes ...Had dinner ... Watched

videos... And above all, play LOL ......But there was a different thing I did from yesterday. I was doing exercises now...

Pull ups , push ups... I was doing greate before I started playing LOL ...... It's my bad that I couldn't resist playing it.

I was and still is too easy to be manipulated .Shame on me . I never change , never learn from mistakes made before.

I don't know whether others are as fragile as me . Now I am very tired , and my mind told me this: please take some

rest and take off your mask .  Ha ha , why so serious ?


Aye , this is me ! An insane man ! Get away from me ! Ha ha !

Oh , it stroke me that I have to post the refined code today. Here it goes , the implementation of QuickSort:

void QuickSort(int *array,int left,int right)
{
	if(left<right){

		int s=Partition(array,left,right);
		QuickSort(array,left,s-1);		//forget to sub s by 1 
		QuickSort(array,s+1,right);
	}
}

int Partition(int *array,int left,int right)
{
	int pivot=array[left];// the pivot whose value will be compared with in the following codes
	
	int i=left,j=right+1;//the start position of the two ways scan 
	
	int tmp;			//variable for storing  temporary number 
	
	do{
		do{
			i+=1;						  //same mistak made , first was i<=right
		}while(array[i]<pivot && i<right);//****************caution , i may advancing beyond the bound of the array************************
		
		do{
			j-=1;						 //same mistak made , first was j >= left 
		}while(array[j]>pivot && j>left);//****************caution , j may advancing beyond the bound of the array************************
		
		//swap array[i] with array[j]
		tmp=array[i];
		array[i]=array[j];
		array[j]=tmp;
	}while(i<j);				//first was  while(i<=j); I spent too much time in find this boundary value 
	
	tmp=array[i]; 
	array[i]=array[j]; 
	array[j]=tmp;
	
	//I swap wrong values in the first place 
	tmp=array[left]; 
	array[left]=array[j]; 
	array[j]=tmp;
	 
	return j;
	
}

My mistakes were hidden in the processing of boundary values and mis-understand in the pseudocode of the

Algorithm. After I tested some values by hands, I finally found those bugs and refined them . Then the code runs

smoothly . It's really a fast Algorithm among other Algorithms which sorts orderable values . I have to tell you that

MergeSort is as fast as QuickSort while sorting large amount of random values . So , chose one of them when you

need to sort large amount of random values.   There are two examples of Dynamic Programming I wanted to show

you ,but time is always not enough for me . So , maybe next time ?

Yeah , that's all for today , thanks for reading and you're welcome to leave comments beblow .

Good night . 微笑



猜你喜欢

转载自blog.csdn.net/cwg2552298/article/details/79673593