MergeSort Implementation with C

Nothing new today , only a few boring classes . Such as Digit Image Process and Computer Network . I felt sleepy in

the morning , crap , I had to sleep early tonight . As I recall , I wasted a day again. I don't have a clear aim to work for.

I don't know what carreer to take in the future . My sister often says that sometimes you need luck to find a proper

job,which I couldn't agree more . That's what makes me so confused and frustrated . I was like a fly with no head , not

knowing my direction and flying here and there . And end up with nothing . I really want to find a goal to fight for.

Which path should I take ? Reverse engineering ? C++ Programming ? Embedded OS Application ?

Artificial Intelligence ? Driver Development ? It's so hard to chose when there are so many paths for you , right ?

No one can should me the way , also I have to find my way . I have to try each way , right ?  No one knows what

would I end up with . Maybe I would be a house constructor , who do the most tiring job in society, from my point

of view .

Really , I did not learn anything today . I felt sorry for myself .

I copied an implementation of an sorting Algorithm named MergeSort, Codes are:

void MergeSort(int * array,int left,int right)
{
	if(left < right){
		int mid=(left + right)/2;
		MergeSort(array,left,mid);
		MergeSort(array,mid+1,right);
		Merge(array,left,mid,right);
	}
}

void Merge(int * array,int left,int mid,int right)
{
	int * tmp=(int*)malloc(sizeof(int)*(right-left+1));
	int i=left,j=mid+1,m=0;
	
	while(i <=mid && j<=right){
		if(array[i]<array[j])
			tmp[m++]=array[i++];
		else
			tmp[m++]=array[j++];
		
	}
	
	while(i <= mid)
		tmp[m++]=array[i++];
	
	while(j <= mid)
		tmp[m++]=array[j++];
	
	for(int n=left,m=0;n<=right;n++,m++)
		array[n]=tmp[m];
	
	free(tmp);
}

This algorithm is said to be very stable , but its drawback is occupying more memory resource .

I used 100 thousand random numbers to compare the running time of Algorithm SelectSort with the so called efficent

Algorithm MergeSort . Here goes the result of MergeSort :

As we can see from the picture showed above , MergeSort only takes about 19 seconds to sort all the 100 thousand

numbers including generating them !

And here is the result of SelectSort:

SelectSort Algorithm taked 32 seconds when sorting the 10 thousand random numbers .

That was really amazing ! Though I did not test for many times , but it's for sure that MergeSort is faster when

sorting large amount of numbers .  I mean , it's really cool . The author of this Algorithm is really a genius !

All right , that's all for today's report . Thanks for your time .

Got classes tomorrow morning , good dream my friends .


猜你喜欢

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