Fast sorting, applied to the Division method.
Let's look at what is the division method?
Divide and conquer law, as the name implies, divide and conquer. The problem is decomposed first, then the problem is solved, and all the separated solutions are combined to get the final solution.
Divide and conquer the law, "punches, trivial, after a close, great things to solve" well, that's it ....
Then to understand the division of the law, then to solve the problem, merge sort. (In fact, the algorithm has so many sorts have been confused, but also unclear, how he called this name, although know the name, but still do not know how to solve the problem, how to solve the problem? It is puzzling ..... Haven't figured out a good way to remember. Had to memorize first.)
All right, nonsense. No more talking, analysis and solving.
Sparks language
Procedure MergeSort (Low,high)
Integer Low,high;
If Low
Then mid<-(low+high)/2;//Split Point
Call MergeSort (Low,mid)//Sort the previous section
Call MergeSort (Mid+1,high)//Sort the latter part
Call Merge (Low,mid,high)//Finally, the sorted order is merged.
endif
End MergeSort
The following are the merged
Procedure Merge (Low,mid,high)//merge as if playing cards is a total of two decks of cards have been sorted, and then two decks of cards integrated into a deck of cards to compare the size of the two decks, placed in another position and finally back to the original position.
Integer L,h,j,low,mid,high
Global A (Low,high); local B (Low,high)
L<-low;h<-low;j<-mid+1
While H<=mid and J<=high do
If A (h) <a (j) then B (i) <-a (h); h<-h+1
else B (i) <-a (j); j<-j+1
endif
i<-i+1;
Repeat
If H>mid then for k<-j to High do//if one of the decks is finished, then the rest of the shot is placed in a deck of cards.
B (i) <-a (k); i<-i+1
Repeat
else for K<-h to mid do
B (i) <-a (k); i<-i+1
Repeat
endif
For K<-low to high do//down to the original position
A (k) <-b (i)
Repeat
End MERGE
The merging sort of algorithm three