归并排序Merge_Sort

#include  " stdafx.h "
#include 
< iostream >

class  Merge_Sort {
private:
    
int* L ;
    
int* R ;
void Merge(int A[], int  p,int  q, int r)
{
      
int n1 = q - p + 1 ;//计算A[p...q]的长度n1
    int n2 = r - q ;//计算长度A[q+1...r],不包括q,因此不加1
     L = new int[n1+2];//L[0]不用,L[n1+1]做哨兵,故多申请2个。
     R = new int[n2+2]; 
    
    
for(int i = 1; i <=n1; i++)
        L[i] 
= A[p+i-1]; //L[1...n1]存放A[p...q]的值
    for(int j = 1; j <= n2; j++)
        R[j] 
= A[q+j];//R[1...n2]存放A[q+1...r]的值

    L[n1
+1= 2147483647;//整型的最大值,作为哨兵。
    R[n2+1= 2147483647;

    i 
= 1;
    j 
= 1;

    
for(int k = p; k <= r; k++)
        
if(L[i] <= R[j])
        
{
            A[k] 
= L[i];
            
++i;
        }

        
else
        
{
            A[k] 
= R[j];
            
++j;
        }

    
}

public:
    Merge_Sort():L(NULL),R(NULL)
{
    }


void Sort(int A[], int p,int  r)
{
    
if(p < r)
    
{
    
int  q = (p + r)/2;
    Sort(A, p, q);
    Sort(A, q
+1, r);
    Merge(A, p, q, r);
    }

}

~Merge_Sort(){
        delete[] L;
        delete[] R;
    }

}
;

int  main( int  argc,  char *  argv[])
{   int B[10= {4522427483699999};
    Merge_Sort p;
    p.Sort(B,
0,9);
    
for(int i = 0; i <= 9; i++)
        std::cout
<<B[i]<<std::endl;
    
return 0;
}

//
 下面的代码是计算类型的范围大小。
#include "stdafx.h"
#include <iostream>
#include <limits>
#include <typeinfo>

template<class T>
class Type{
    public:
    static void print(){
        std::cout<<typeid(T).name() <<"   : range is ("
            <<std::numeric_limits<T>::min() <<" , "
            <<std::numeric_limits<T>::max() <<")/n";
    }
};

int main(int argc, char* argv[])
{  
    Type<char>::print();
    Type<short>::print();
    Type<int>::print();
    Type<long>::print();
    Type<float>::print();
    Type<double>::print();
    Type<long double>::print();
    Type<unsigned>::print();
    return 0;
}

char   : range is (€ , )
short   : range is (-32768 , 32767)
int   : range is (-2147483648 , 2147483647)
long   : range is (-2147483648 , 2147483647)
float   : range is (1.17549e-038 , 3.40282e+038)
double   : range is (2.22507e-308 , 1.79769e+308)
long double   : range is (2.22507e-308 , 1.79769e+308)
unsigned int   : range is (0 , 4294967295)
Press any key to continue
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yshuise

权术横行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值