Merge Sorting of data structures and algorithms

Source: Internet
Author: User

Merge Sorting: the sub-array sequence of each merge is ordered.

Time Complexity: O (nlog2n), the space cost is O (n)

The Code is as follows:

Int a [10] = {,}, B [10] = {0}; void merge (const int left, const int mid, const int right) {int s1 = left; int s2 = mid; int s3 = right, k, t = left; for (k = left; k <= mid; k ++) B [k] = a [k]; for (k = right; k> mid; k --) // Reverse Order B [right + mid + 1-k] = a [k]; while (t <= right) {if (B [s1] <= B [s3]) a [t ++] = B [s1 ++]; else a [t ++] = B [s3 --] ;}} void mergesort (int left, int right) {if (left> = right) return; int mid = (left + right)/2; mergesort (left, mid); mergesort (mid + 1, right ); merge (left, mid, right);} void show () {for (int I = 0; I <10; I ++) {cout <a [I] <";}cout <endl ;}int main () {show (); mergesort (); show (); return 0 ;}

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.