#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] = {452, 2, 4, 2, 7, 4, 8, 3, 6, 99999};
Merge_Sort p;
p.Sort(B,0,9);
for(int i = 0; i <= 9; i++)
std::cout<<B[i]<<std::endl;
return 0;
}
//
#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] = {452, 2, 4, 2, 7, 4, 8, 3, 6, 99999};
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