Rewrite way merge sort

Using two arrays each other as auxiliary arrays to implement, the first two-way division, merging again starts from a single element. code show as below:

/ * 
    Road merge sort using a recursive approach, it is first divided into two portions of the entire queue, once the two portions divided by two operations, 
    until each portion contains only one element up, and then sequentially these element contains only a part of started merging, in turn upwards and then recursion 
    backtracking, and finally returns the sorted queue, the sort is complete. 
    Time complexity of n-* log2n   
* / 
#include <the iostream>
 the using  namespace STD; 

void Merge ( int * B, int * T, int S, int MID, int E) {
     int I = S;
     int J = MID + . 1 ;
     int = IDX S;
     the while (I <= MID && J <= E) 
    { 
        IF (B [I] < B [J])
        {
            t[idx++]=b[i++];
        }
        else
        {
            t[idx++]=b[j++];
        }
    }
    while(i<=mid)
    {
        t[idx++]=b[i++];
    }
    while(j<=e)
    {
        t[idx++]=b[j++];
    }
}
void TwoGuiBing(int * a,int * t,int s,int e){
    if(s==e)
    {
        t[s]=a[s];
        return;
    }
    int mid=(s+e)/2;
    TwoGuiBing(t,a,s,mid);
    TwoGuiBing(t,a,mid+1,e);
    merge(a,t,s,mid,e);
    
    for(int i=s;i<=e;i++)
    {
        cout<<t[i]<<" ";
        //a[i]=t[i];
    }
    cout<<endl;
    
}
int main(){
    int s[10]={4,1,5,2,4,3,98,6,65,1};//1 1 2 3 4 4 5 6 65 98 
    int t[10];
    for(int i=0;i<10;i++)
    {
        t[i]=s[i];
    }
    TwoGuiBing(s,t,0,9);
    for(int i=0;i<10;i++){
        cout<<t[i]<<" ";
    }
    cout<<endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/JsonZhangAA/p/11566241.html