冒泡排序法排序操作模板
发布时间:2020-12-13 20:10:18 所属栏目:PHP教程 来源:网络整理
导读:对int型,double型,char型数组元素进行排序。 #includeiostreamusing namespace std;# include string.htemplate class stype void bubble(stype *item,int count);void main(){char str[]=ahsdkcfgmwdlx;bubble(str,(int)strlen(str));coutThe sorted strin
对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,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;
}
} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |