- /**************************************************
- *filename:sort.cpp *
- *author:dongmeiZhao *
- *date:2011-02-16 *
- *description: this is a example about template *
- **************************************************/
- #include <iostream>
- using namespace std;
- template <class T>
- void smallToBig(T a[], T n)
- {
- T temp;
- //采用冒泡法进行排序
- for (int i=0; i<n-1; i++)
- {
- for (int j=0; j<n-i-1; j++)
- {
- if (a[j]>a[j+1])
- {
- temp = a[j];
- a[j] = a[j+1];
- a[j+1] = temp;
- }
- }
- }
- }
- int main()
- {
- int n;//n个数据
- cout<<"please input the number of data:1--10"<<endl;
- cin>>n;
- //输入n个数据
- int dataBuf[10];
- int dataTemp;
- for (int num=0; num<n; num++)
- {
- cout<<"please input the"<<num<<"data"<<endl;
- cin>>dataTemp;
- dataBuf[num] = dataTemp;
- }
- smallToBig(dataBuf,n);//排序
- //从小到大输出数据
- for (int k=0; k<n; k++)
- {
- cout<<dataBuf[k]<<'\t';
- }
- return 0;
- }