冒泡排序法排序操作模板
來源:程序員人生 發布時間:2015-05-15 08:00:03 閱讀次數:3513次
對int型,double型,char型數組元素進行排序。
#include<iostream>
using namespace std;
# include <string.h>
template <class stype> void bubble(stype *item,int count);
void main()
{
char str[]="ahsdkcfgmwdlx";
bubble(str,(int)strlen(str));
cout<<"The sorted string is: "<<str<<endl;
int num[]={2,3,1,4,6,8,4,1};
bubble(num,8);
cout<<"The sorted numbers are: ";
for(int i=0;i<8;i++)
cout<<num[i]<<" ";
cout<<endl;
double num1[]={1.2,3.5,1.3,5.2,2.3};
bubble(num1,5);
cout<<"The sorted numbers are: ";
for(i=0;i<5;i++)
cout<<num1[i]<<" ";
cout<<endl;
}
template<class stype>
void bubble(stype *item,int count)
{
register i,j;
stype t;
for(i=1;i<count;i++)
for(j=count⑴;j>=i;j--)
if(item[j⑴]>item[j])
{
t=item[j⑴];
item[j⑴]=item[j];
item[j]=t;
}
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈