#include <iostream>
#include <string>
using namespace std;
#define NUM 10000
#define dim(x) (sizeof(x) / sizeof(x[0]))
#define SWAP(a, b) ((a) = (a) ^ (b); (b) = (a) ^ (b); (a) = (a) ^ (b);)
#define STSWAP(a,b)(string temp; temp = a; a = b; b = temp;)
template<class T, int num>
class CData
{
private:
T* m_data;
private:
void DescendingSort(T Array[]);
void QuickSort(T a[], int left, int right);
int Partition(T a[], int left, int right);
int CompStr(string str1, string str2);
public:
CData();
CData(T Array[]);
~CData();
void sort();
void show();
T* GetData();
};
template<class T, int num>
int CData<T,num>::CompStr(string str1, string str2)
{
return str1.compare (str2);
}
template<class T, int num>
CData<T,num>::CData()
{
m_data = NULL;
}
template<class T, int num>
CData<T,num>::CData(T Array[])
{
m_data = new T[num * sizeof(Array)];
memcpy(m_data,Array,num * sizeof(Array));
}
template<class T, int num>
CData<T,num>::~CData()
{
if (m_data != NULL)
{
delete[] m_data;
m_data = NULL;
}
}
template<class T, int num>
void CData<T,num>::show()
{
for (int i = 0; i < num; ++i)
cout << m_data[i] << " ";
cout << endl;
}
template<class T, int num>
T* CData<T,num>::GetData()
{
return m_data;
}
//插排
template<class T, int num>
void CData<T,num>::DescendingSort(T Array[])
{
int i, j, length;
T key;
T* pointer;
length = num;
pointer = Array;
for(j = 1; j < length; j ++)
{
i = j - 1;
key = pointer[j];
#ifndef STRING
while(i >= 0 && pointer[i] < key)
{
pointer[i + 1] = pointer[i];
i--;
}
#endif
#ifdef STRING
while(i >= 0 && this->CompStr(pointer[i],key))
{
pointer[i + 1] = pointer[i];
i--;
}
#endif
pointer[i + 1] = key;
}
}
template<class T, int num>
int CData<T,num>::Partition(T a[], int left, int right)
{
int i, j;
T key;
i = left;
j = right;
key = a[left];
do
{
#ifndef STRING
while (a[j] >= key && i < j)
{
j--;
}
if (i < j) SWAP(a[i], a[j]);
while (a[i] <= key && i < j)
{
i++;
}
if (i < j) SWAP(a[i], a[j]);
} while (i < j);
#endif
#ifdef STRING
while (this->CompStr(a[j],key) && i < j)
{
j--;
}
if (i < j) STSWAP(a[i], a[j]);
while (this->CompStr(a[i],key) && i < j)
{
i++;
}
if (i < j) STSWAP(a[i], a[j]);
} while (i < j);
#endif
return j;
}
//快排
template<class T, int num>
void CData<T,num>::QuickSort(T a[], int left, int right)
{
if (left >= right)return;
if (left + 1 == right)
{
#ifndef STRING
if(a[left] > a[right])SWAP(a[left], a[right]);
#endif
#ifdef STRING
if(this->CompStr(a[left],a[right]))STSWAP(a[left], a[right]);
#endif
return;
}
int j = Partition(a, left, right);
QuickSort(a, left, j - 1);
QuickSort(a, j + 1, right);
}
template<class T, int num>
void CData<T,num>::sort()
{
if (num <= 0)
return;
if (num <= NUM)
this->DescendingSort (m_data);
if (num > NUM)
this->QuickSort (m_data,0,num-1);
}
int main()
{
//1、测试int
int a[] = {12, 34, 4, 45, 2, 46, 8, 13, 56, 0};
CData<int,dim(a)> idat1(a);
idat1.sort ();
idat1.show ();
//2、测试char
char c[] = {'d', 'w', 'y', 'a', 'f', 'e', 'h', 'u', 'c', 'd'};
CData<char,dim(c)> cdat1(c);
cdat1.sort ();
cdat1.show ();
//3、测试string 定义宏STRING,同时将1、2注册掉
string s[] = {"xiaoya", "weige", "gesehng", "huanzai", "rongmei", "yongge", "zhengpeng", "luomei", "sisi", "binbing"};
CData<string,dim(s)> szdat1(s);
szdat1.sort ();
szdat1.show ();
return 0;
}