Merge sort algorithm C code implementation

Source: Internet
Author: User

Merge sort is another type of sorting method. The meaning of merging is to combine two or more ordered data sequences into a new ordered data sequence, therefore, it is also called the merge algorithm. The basic idea is to assume that array A has n elements, and array a is composed of N ordered subsequences. The length of each subsequence is 1, then merge them in two ways to obtain an n/2 ordered subsequences with a length of 2 or 1, and then merge them in two, it is worthwhile to obtain a sequence of ordered data whose length is N. This sorting method is called 2-way merge sorting.

For example, if array A has 7 data records, which are 49 38 65 97 76 13 27, then the operation process using the Merge Sorting Algorithm is 7:
Initial Value [49] [38] [65] [97] [76] [13] [27]
It is regarded as composed of seven subsequences with a length of 1.
[38 49] [65 97] [13 76] [27] after the first merge
It is regarded as composed of four subsequences with a length of 1 or 2.
[38 49 65 97] [13 27 76] after the second merge
It is considered to be composed of two subsequences with a length of 4 or 3.
[13 27 38 49 65 76 97] after the third merge
The core operation of the merge algorithm is to combine two adjacent sequential sequences in one-dimensional array into one ordered sequence. Merge algorithms can also be implemented using recursive algorithms. The form is simple, but the practicality is poor. The number of merging operations of the merge algorithm is a very important amount. When the array contains 3 to 4 elements, the number of merging operations is twice. When there are 5 to 8 elements, the number of merging operations is three. When there are 9 to 16 elements, the number of merging operations is four. According to this rule, when there are n subsequences, we can infer that the number of merging times is X (2> = N, the minimum X that meets this condition ).

The time complexity is O (nlogn). The auxiliary storage space required is O (n)
The merge algorithm is as follows:

# Include <stdio. h>

  1. # Include <stdlib. h>
  2. Typedef int rectype; // type of the element to be sorted
  3. Void Merge (rectype * r, int low, int M, int high)
  4. {
  5. // Combine two ordered sub-files R [low.. m) and R [M + 1 .. High] into an ordered sub-file R [low .. High]
  6. Int I = low, j = m + 1, P = 0; // set the Initial Value
  7. Rectype * R1; // R1 is a local vector
  8. R1 = (rectype *) malloc (high-low + 1) * sizeof (rectype ));
  9. If (! R1)
  10. {
  11. Return; // failed to apply for Space
  12. }
  13. While (I <= M & J <= high) // when the two sub-files are not empty, the small ones are output to R1 [p ].
  14. {
  15. R1 [p ++] = (R [I] <= R [J])? R [I ++]: R [J ++];
  16. }
  17. While (I <= m) // If the 1st sub-files are not empty, copy the remaining records to r1
  18. {
  19. R1 [p ++] = R [I ++];
  20. }
  21. While (j <= high) // If the 2nd sub-files are not empty, copy the remaining records to r1
  22. {
  23. R1 [p ++] = R [J ++];
  24. }
  25. For (P = 0, I = low; I <= high; P ++, I ++)
  26. {
  27. R [I] = R1 [p]; // after merging, copy the result back to R [low... high]
  28. }
  29. }
  30. Void mergesort (rectype R [], int low, int high)
  31. {
  32. // Sort R [low .. High] by means of the division and Control Law.
  33. Int mid;
  34. If (low
  35. {// The Interval Length is greater than 1
  36. Mid = (low + high)/2; // Decomposition
  37. Mergesort (R, low, mid); // recursively sorts R [low... mid]
  38. Mergesort (R, Mid + 1, high); // recursively sorts R [Mid + 1 .. High]
  39. Merge (R, low, mid, high); // combine two ordered zones into one ordered Zone
  40. }
  41. }
  42. Void main ()
  43. {
  44. Int A [7] = {, 27}; // sort the eight elements
  45. Int low = 0, high = 6; // initialize the values of low and high
  46. Printf ("before merge sort :");
  47. For (INT I = low; I <= high; I ++)
  48. {
  49. Printf ("% d", a [I]); // output test
  50. }
  51. Printf ("/N ");
  52. Mergesort (A, low, high );
  53. Printf ("after merge sort :");
  54. For (I = low; I <= high; I ++)
  55. {
  56. Printf ("% d", a [I]); // output test
  57. }
  58. Printf ("/N ");
  59. }

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.