Two-way merge sort (also called merge sort)

Source: Internet
Author: User

The following figure shows the two-way merge process.

The core code of the two-way merge is the merge () function

It merges 2 segmented arrays together in an orderly fashion.

In Array A,

From P to Q is an array, from Q to R is another array

So how do you combine the 2 arrays together in order to group A new array a?

Steps:

First step: Open an array of L, storing the elements of P to Q (that is, the left array that needs to be merged)

Part II: Open an array r, storing the elements of Q to R (that is, the right-hand array that needs to be merged)

The third step: The Last of the 2 arrays is also added an infinite number (can be expressed in 0x7fff), so the open array space is more than 1 characters of space

Fourth step: L and R numbers are compared one by one, the smaller first placed in the array a (starting from the array a "0" to store, and then overwrite the original number), and then the smaller array pointer moves backwards, pointing to the next one and another array comparison

[CPP]View PlainCopy
  1. The first parameter is the array that needs to be sorted, and the 2nd parameter is the first array of the split start element subscript
  2. The 3rd parameter is the subscript of the last 1 elements of the first array of the split
  3. The 4th parameter is the subscript for the last 1 elements of an array
  4. void Merge (int *a,int p,int q,int r)
  5. {
  6. int n1,n2,i,j,k,g;
  7. n1=q-p+1;
  8. N2=r-q;
  9. int *l,*r;
  10. l= (int *) malloc (sizeof (int) * (n1+1));
  11. r= (int *) malloc (sizeof (int) * (n2+1));
  12. L[N1]=0X7FFF; //Open the left and right 2 arrays the last 1 numbers are set to the maximum value
  13. R[N2]=0X7FFF;
  14. g=0;
  15. For (i=p;i<=q;i++)
  16. {
  17. L[g]=a[i];
  18. g++;
  19. }
  20. g=0;
  21. For (i=q+1;i<=r;i++)
  22. {
  23. R[g]=a[i];
  24. g++;
  25. }
  26. //Compare the left and right arrays, and write the smaller values to the original array
  27. j=k=0;
  28. For (i=p;i<=r;i++)
  29. {
  30. if (l[j]<r[k])
  31. {
  32. A[I]=L[J];
  33. j + +;
  34. }
  35. Else
  36. {
  37. A[I]=R[K];
  38. k++;
  39. }
  40. }
  41. }


Full code:

[CPP]View PlainCopy
    1. #include <iostream>
    2. Using namespace std;
    3. The first parameter is the array that needs to be sorted, and the 2nd parameter is the first array of the split start element subscript
    4. The 3rd parameter is the subscript of the last 1 elements of the first array of the split
    5. The 4th parameter is the subscript for the last 1 elements of an array
    6. void Merge (int *a,int p,int q,int r)
    7. {
    8. int n1,n2,i,j,k,g;
    9. n1=q-p+1;
    10. N2=r-q;
    11. int *l,*r;
    12. l= (int *) malloc (sizeof (int) * (n1+1));
    13. r= (int *) malloc (sizeof (int) * (n2+1));
    14. L[N1]=0X7FFF; //Open the left and right 2 arrays the last 1 numbers are set to the maximum value
    15. R[N2]=0X7FFF;
    16. g=0;
    17. For (i=p;i<=q;i++)
    18. {
    19. L[g]=a[i];
    20. g++;
    21. }
    22. g=0;
    23. For (i=q+1;i<=r;i++)
    24. {
    25. R[g]=a[i];
    26. g++;
    27. }
    28. //Compare the left and right arrays, and write the smaller values to the original array
    29. j=k=0;
    30. For (i=p;i<=r;i++)
    31. {
    32. if (l[j]<r[k])
    33. {
    34. A[I]=L[J];
    35. j + +;
    36. }
    37. Else
    38. {
    39. A[I]=R[K];
    40. k++;
    41. }
    42. }
    43. }
    44. void MergeSort (int *a,int p,int r)
    45. {
    46. int q;
    47. if (p<r) //When the first element is less than the last 1 elements, continue to perform recursion until only one element is left (parameter p=r)
    48. {
    49. q= (P+R)/2;
    50. MergeSort (A,P,Q);
    51. MergeSort (A,Q+1,R);
    52. Merge (A,P,Q,R);
    53. }
    54. }
    55. void Main ()
    56. {
    57. int a[5]={5,3,4,23,11};
    58. MergeSort (a,0,4);
    59. For (int i=0;i<5;i++)
    60. cout<<a[i]<<endl;
    61. System ("pause");
    62. }

Two-way merge sort (also called merge sort)

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.