/* ArrayBubbleSort.hpp */
#ifndef _ARRAY_BUBBLE_SORT_H_
#define _ARRAY_BUBBLE_SORT_H_
template<typename T>
bool BubbleSort(T *pInput, int nLen)
{
int i = 0;
int j = 0;
bool bChange = false;
T tTemp;
if(!pInput)
{
return false;
}
for(i=0; i<nLen-1; i++)
{
bChange = false;
for(j=0; j<nLen-1-i; j++)
{
if(pInput[j]>pInput[j+1])
{
tTemp = pInput[j+1];
pInput[j+1] = pInput[j];
pInput[j] = tTemp;
bChange = true;
}
}
if(!bChange)
{
break;
}
}
return true;
}
#endif
/* BubbleSort.cpp */
#include "ArrayBubbleSort.hpp"
#include <iostream>
using namespace std;
int main()
{
int a[10] = {1,4,7,2,5,8,3,6,9,0};
int i = 0;
if(BubbleSort<int>(a,10)==false)
{
cout<<"排序失败"<<endl;
}
for(i=0; i<10; i++)
{
cout<<a[i]<<"\t";
}
cout<<endl;
return 0;
}