Merge Sorting, Merge Sorting java

Source: Internet
Author: User

Merge Sorting, Merge Sorting java
Merge two or more ordered columns (or ordered tables) into a sequence that is still ordered (ordered tables). This operation is called a merge operation. This method is often used to merge multiple ordered data files into an ordered data file. If two ordered tables are merged into an ordered table, it is called a two-way merge. Similarly, there are three ways to merge and four ways to merge. Two-way merge is relatively simple, so we only discuss two-way merge. For example, there are two ordered tables: (,) and (,). After merging, the obtained ordered tables are ). The merging process is: Compare the sizes of A [I] And A [j]. If A [I] is less than or equal to A [j], then, copy the element A [I] In the first ordered table to R [k], and Add 1 to I and k respectively, even if they indicate the last unit, otherwise, the elements A [j] In the second ordered table are copied to R [k], and 1 is added to j and k respectively. In this way, the loop continues until one of the ordered tables is completed, then copy the remaining elements in another ordered table to the Unit from subscript k to subscript t in R.

1 # include <iostream> 2 using namespace std; 3 int a [10001], r [10001], n, I; // a is the array to be sorted, r is a temporary Array 4 void mergesort (int s, int t) // sort the unordered data in the [s, t] interval by merging and sorting 5 {6 int m, I, j, k; 7 if (s = t) 8 return; // if there is only one data range, 9 m = (s + t)/2 is not required; // obtain the midpoint 10 mergesort (s, m) of the interval; // sort the left-side intervals by the midpoint of the midpoint. 11 mergesort (m + 1, t ); // sort the intervals on the right by the midpoint of 12 I = s; // The following is a merge (merge) operation 13 j = m + 1; 14 k = s; 15 while (I <= m & j <= t) // The two subsequences are merged from small to large, until a column ends 16 {17 if (a [I] <= a [j]) 18 {r [k] = a [I]; 19 I ++; k ++; 20} 21 else22 {23 r [k] = a [j]; 24 j ++; k ++; 25} 26} 27 while (I <= m) // * Add the remaining elements of the Left subsequence to * 28 {29 r [k] = a [I]; 30 I ++; 31 k ++; 32} 33 while (j <= t) // Add the remaining elements of the sub-sequence on the right to 34 {35 r [k] = a [j]; 36 j ++; 37 k ++; 38} 39 for (I = s; I <= t; I ++) // put the merged ordered data back into array a 40 a [I] = r [I]; 41} 42 int main () 43 {44 cin> n; 45 for (I = 1; I <= n; I ++) // read n data to be sorted 46 cin> a [I]; 47 mergesort (1, n); // sort the unordered data in the [1, n] interval by merging and sorting 48 for (I = 1; I <= n; I ++) // output n ordered Data 49 cout <a [I] <""; 50 cout <endl; 51}

 

 

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.