Merge_sort Merge sort C + + implementation

Source: Internet
Author: User

The following implementation of the merge sort, the first part of the Sentinel (algorithm from the introduction of the algorithm), the second part does not contain Sentinel

The first part, containing the Sentinel's merge sort:

#include <iostream>using namespace std; void mymerge (int * A, int p, int q, int r) {    int n1 = Q-p + 2;    int n2 = r-q + 1;    int l[n1];    int r[n2];    for (int i = 0; i < n 1-1; ++i)         L[i] = a[p + i];    L[n1-1] = int_max;    for (INT j = 0; j < n2-1; ++J)         R[J] = a[q + j + 1];    r[n2-1] = int_max;    INT i = 0;    Int J = 0;    for (int k = p; k <= R; ++k)       &NBSP;A[K] = L[i] < R[j]? L[i++]: r[j++];}  void merge_sort (int *a, int p, int r) {    if (R > P)     {        INT q = ( R + P)/2;        Merge_sort (A, p, q);        Merge_sort (A, q+1, R);        Mymerge (A, p, Q, R);   }} int main () {    int b[4] = {4,3,2,1};    MERGE _sort (b,0,3);    for (int i = 0; i < 4; ++i)         cout << B[i] << endl;     Retu RN 0;} -----------------------------------------------------
#include <iostream>using namespace std; void mymerge (int *a, int p, int q, int r) {    int n1 = q-p + 2;    int n2 = r-q + 1;    int l[n1];    int r[n2];    for (int i = 0; i < N1 -1; + + i)     {        L[i] = a[p + i];   }    L[n1-1] = 0;    F or (int j = 0; J < n2-1; + + j)     {        R[J] = a[q + 1 + j];   }  &NB Sp R[N2-1] = 0;    int i = 0;    int j = 0;    for (int k = p; k <= R; ++k)     {& nbsp       A[K] = L[i] > r[j]? l[i++]: r[j++];   }} void merge_descsort (int *a, int p, int R) {    if (R > P)     {& nbsp       INT q = (r + P)/2;        Merge_descsort (A, p, q);        M Erge_descsort (A, q + 1, R);        Mymerge (A, p, Q, R); &nbsP  }} int Main () {    int b[] = {1,2,3,4,5};    Merge_descsort (b, 0, 4);    for (int i = 0; I < 5; ++i)     {        cout << B[i] << endl;   }     return 0;}   

  The second part, does not contain the Sentinel's merge sort:

#include <iostream>using namespace std; void myMERGE2 (int *a, int p, int q, int r) {    int n1 = Q-p + 1;    int n2 = r-q;    int l[n1];    int r[n2];    for (int i = 0; i < N1; + +i)         L[i] = a[p + i];    for (int j = 0; J < N2; ++j)         R[J ] = a[q + 1 + j];    int i = 0;    int j = 0;    int k = p;    while (I < N1 &am p;& J < N2)         a[k++] = L[i] < R[j]? l[i++]:r[j++];    while (I < N1)         a[k++] = l[i++];    while (J < N2) &nbs P       a[k++] = r[j++];}  void merge_sort2 (int *a, int p, int r) {    if (P < r)     {        INT q = (r + P)/2;        Merge_sort2 (A, p, q);        Merge_sort2 (A, q + 1, r);  & nbsp     myMERGE2 (A, p,Q, R);   }} int main () {    int b[] = {5,4,3,2,1};    Merge_sort2 (b,0,4);    for (int i = 0; i < 5; ++i)         cout << B[i] << endl;     return 0;}

Merge_sort Merge sort C + + implementation

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.