The merging sort of algorithm three

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.