算法排序 归并 理解性写

 
 
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
using namespace std;



void myswap(int a[],int l,int m,int r,int *&b)
{
    int p = 0;
    int i=l, j = m + 1;
    while (i <= m&&j <= r)
    {
        if (a[i] < a[j])
        {
            b[p++] = a[i++];
        }
        else
        {
            b[p++] = a[j++];
        }
    }
    while (i <= m){ b[p++] = a[i++]; }
    while (j <= r){ b[p++] = a[j++]; }

  
    memcpy(a+l,b,p*sizeof(int));

}

void mysort(int a[], int l,int r,int *&b)
{
    int m;
    if (l < r)
    {
        m = (l + r) / 2;
        mysort(a, l, m,b);
        mysort(a, m + 1, r,b);
        myswap(a,l,m,r,b);

    }

}


int main()
{
    int *b = new int[6];
    memset(b, 0, 6 * 4);
    int a[5] = { 1, 45, 6, 67, 78 };
    mysort(a,0,5-1,b);
    delete[]b;
    for (int i = 0; i < 5; i++)cout <<a[i] << " ";

    system("pause");
    return 2;
}


你可能感兴趣的:(算法排序 归并 理解性写)