Start from sorting (3) Merge Sorting

Source: Internet
Author: User

Merge Sorting is an effective Sorting Algorithm Based on merge operations. This algorithm is a very typical application of Divide and Conquer. The merge operation process is as follows: 1. apply for a space so that the size is the sum of two sorted sequences. This space is used to store the merged sequence 2. set two pointers. The initial position is the start position of the two sorted sequences. compare the elements pointed to by the two pointers, select a relatively small element into the merging space, and move the pointer to the next position 4. repeat Step 3 until a pointer reaches the end of the sequence. copy all the remaining elements of another sequence directly to the end of the merge sequence. Simply put, the merge sequence is to continuously divide the sequence, until each sequence has only one element (the sequence of an element must be ordered), and then these ordered sequences are merged to form a new ordered sequence. Finally, they are merged into all ordered sequences. This Wiki diagram intuitively explains Merge Sorting: Worst time complexity O (n logn) optimum time complexity O (n) Average time complexity O (n logn) Stability: stable implementation:

# Include <iostream> using namespace std; // The merge operation. There will be two sequential sequences: num [start... mid] And num [mid... end. // Merge to the sorted sequence, and then copy back to the original array void merge (int num [], int start, int mid, int end, int sorted []) {int I = start, j = mid + 1; int m = mid, n = end; int k = 0; // compare two elements at a time, until a sequence reaches the end while (I <= m & j <= n) {if (num [I] <= num [j]) sorted [k ++] = num [I ++]; else sorted [k ++] = num [j ++];} // copy the remaining elements in the sequence directly to the end of the sorted sequence while (I <= m) sorted [k ++] = num [I ++]; while (j <= n) sorted [k ++] = num [j ++]; // write the sorted sequence back to the original array f Or (I = 0; I <k; I ++) num [start + I] = sorted [I];} void mergesort (int a [], int start, int end, int sorted []) {if (start <end) {// grouping int mid = (start + end)/2; mergesort (a, start, mid, sorted); mergesort (a, mid + 1, end, sorted); merge (a, start, mid, end, sorted) ;}} bool MergeSort (int num [], int n) {int * p = new int [n]; if (p = NULL) return false; mergesort (num, 0, n-1, p); delete [] P; return true;} // simple test. It is recommended that the data be not too big. It is in screen flushing. o (others □others) o int main () {int n; // test the sorting of n random numbers while (cin> n) {int * p = new int [n]; if (! P) {cout <"Failed... "<endl; continue;} for (int I = 0; I <n; ++ I) p [I] = rand (); bool flag = MergeSort (p, n); if (! Flag) {cout <"Failed... "<endl; continue;} for (int I = 0; I <n; ++ I) cout <p [I] <''; cout <endl; delete [] p;} return 0 ;}

 

 

Related Article

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.