Merge sort or recursion (originally difficult to understand is because of recursion), so the first not to say recursion is not difficult to find. (Sum,, pity already said, OK, first when I did not say = =)
So let's start with a question: How to merge two ordered arrays into a new ordered array?
A: This is very simple, as long as the first number from the comparison of two series, who is the first to take who, after taking the corresponding sequence to delete the number. Then the comparison, if there are several columns empty, then directly the data of the other sequence can be taken out in turn.
(This answer to the self-understanding of the person's blog, for the prevention of infringement posted on his website: http://blog.csdn.net/morewindows/article/details/6678165/)
So knowing how to merge two arrays, it is perfectly possible to write a function that implements the function of merging an array:
void Mergearray (int *a,int left,int mid,int right,int *temp)
The function is to combine the two "arrays" of A[left]~a[mid] and A[mid+1]~a[right] into an array, and then replace the position of the previous array of a. Where temp is a temporary space is necessary.
The code is as follows:
1 voidMergearray (int*a,intLeftintMidintRightint*temp)2 {3 inti,j,m,n,k;4I=Left ;5J=mid+1;6m=mid;7n=Right ;8k=0;9 while(i<=m&&j<=N) {Ten if(a[i]<A[j]) { Onek++; Atemp[k]=A[i]; -i++; - } the Else{ -k++; -temp[k]=A[j]; -J + +; + } - } + while(i<=m) { Ak++; attemp[k]=A[i]; -i++; - } - while(j<=N) { -k++; -temp[k]=A[j]; inJ + +; - } to for(i=1; i<=k;i++){ +a[left+i-1]=Temp[i]; - } the}
Well, now that we know how to merge in order, the point is coming. How do you get the array in order? Because we know, to merge order, the premise is orderly Ah!
The answer I think you have guessed--recursion.
We might as well write a function to implement the merge sort function void mergesort (int *a,int left,int right,int *temp)
If you read my Hanoi blog, you should now get used to me
To make an array ordered void mergesort (int *a,int left,int right,int *temp), it is divided into the following steps:
First step: Let the first half of the array be ordered void MergeSort (A,LEFT,MID,TEMP);
Step two: Let the second half of the array be ordered void MergeSort (A,MID+1,RIGHT,TEMP);
Step three: Merge two ordered arrays to generate a new ordered void Mergearray (a,left,mid,right,temp).
So it's done. If it is, you can refer to the following C language code:
1#include <stdio.h>2#include <stdlib.h>3 4 voidMergearray (int*a,intLeftintMidintRightint*temp)5 {6 inti,j,m,n,k;7I=Left ;8J=mid+1;9m=mid;Tenn=Right ; Onek=0; A while(i<=m&&j<=N) { - if(a[i]<A[j]) { -k++; thetemp[k]=A[i]; -i++; - } - Else{ +k++; -temp[k]=A[j]; +J + +; A } at } - while(i<=m) { -k++; -temp[k]=A[i]; -i++; - } in while(j<=N) { -k++; totemp[k]=A[j]; +J + +; - } the for(i=1; i<=k;i++){ *a[left+i-1]=Temp[i]; $ }Panax Notoginseng } - the voidMergeSort (int*a,intLeftintRightint*temp) + { A if(left<Right ) { the intMid= (left+right)/2; + mergesort (a,left,mid,temp); -MergeSort (a,mid+1, right,temp); $ Mergearray (a,left,mid,right,temp); $ } - } - the intMain () - {Wuyi intN; thescanf"%d",&n); - int*a=malloc(nsizeof(int)); Wu int*p=malloc(nsizeof(int)); - inti; About for(i=1; i<=n;i++){ $scanf"%d", A +i); - } -MergeSort (A,1, n,p); - for(i=1; i<=n;i++){ Aprintf"%d:%d\n", I,a[i]); + } the Free(a); - Free(p); $ return 0; the}
Merge sort (read someone else's blog understand also write a blog, hope this does not count plagiarism ~)