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
- 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
- The 3rd parameter is the subscript of the last 1 elements of the first array of the split
- The 4th parameter is the subscript for the last 1 elements of an array
- void Merge (int *a,int p,int q,int r)
- {
- int n1,n2,i,j,k,g;
- n1=q-p+1;
- N2=r-q;
- int *l,*r;
- l= (int *) malloc (sizeof (int) * (n1+1));
- r= (int *) malloc (sizeof (int) * (n2+1));
- L[N1]=0X7FFF; //Open the left and right 2 arrays the last 1 numbers are set to the maximum value
- R[N2]=0X7FFF;
- g=0;
- For (i=p;i<=q;i++)
- {
- L[g]=a[i];
- g++;
- }
- g=0;
- For (i=q+1;i<=r;i++)
- {
- R[g]=a[i];
- g++;
- }
- //Compare the left and right arrays, and write the smaller values to the original array
- j=k=0;
- For (i=p;i<=r;i++)
- {
- if (l[j]<r[k])
- {
- A[I]=L[J];
- j + +;
- }
- Else
- {
- A[I]=R[K];
- k++;
- }
- }
- }
Full code:
[CPP]View PlainCopy
- #include <iostream>
- Using namespace std;
- 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
- The 3rd parameter is the subscript of the last 1 elements of the first array of the split
- The 4th parameter is the subscript for the last 1 elements of an array
- void Merge (int *a,int p,int q,int r)
- {
- int n1,n2,i,j,k,g;
- n1=q-p+1;
- N2=r-q;
- int *l,*r;
- l= (int *) malloc (sizeof (int) * (n1+1));
- r= (int *) malloc (sizeof (int) * (n2+1));
- L[N1]=0X7FFF; //Open the left and right 2 arrays the last 1 numbers are set to the maximum value
- R[N2]=0X7FFF;
- g=0;
- For (i=p;i<=q;i++)
- {
- L[g]=a[i];
- g++;
- }
- g=0;
- For (i=q+1;i<=r;i++)
- {
- R[g]=a[i];
- g++;
- }
- //Compare the left and right arrays, and write the smaller values to the original array
- j=k=0;
- For (i=p;i<=r;i++)
- {
- if (l[j]<r[k])
- {
- A[I]=L[J];
- j + +;
- }
- Else
- {
- A[I]=R[K];
- k++;
- }
- }
- }
- void MergeSort (int *a,int p,int r)
- {
- int q;
- 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)
- {
- q= (P+R)/2;
- MergeSort (A,P,Q);
- MergeSort (A,Q+1,R);
- Merge (A,P,Q,R);
- }
- }
- void Main ()
- {
- int a[5]={5,3,4,23,11};
- MergeSort (a,0,4);
- For (int i=0;i<5;i++)
- cout<<a[i]<<endl;
- System ("pause");
- }
Two-way merge sort (also called merge sort)