#include <iostream>
using namespace std;
/***************冒泡排序算法*******************/
template<class T>
void mySort(T str[],int n){
int i=n-1; //此处的i可以理解为完成排序需要进行比较的数字的个数
while (i>0)
{
int lastIndex=0; //设置最小下标
for (int j=0;j<i;j++)
{
if (str[j]>str[j+1])
{
myswap(str[j],str[j+1]);
lastIndex=j;
}
}
i=lastIndex;
}
}
template<class T>
void myswap(T &i,T &j){
T flag=i;
i=j;
j=flag;
}
void main()
{
int str[]={1,4,2,3,6,5};
mySort(str,6);
for (int i=0;i<6;i++)
{
cout<<str[i]<<" ";
}
cout<<endl;
}
排序算法之冒泡排序
最新推荐文章于 2024-09-01 20:28:04 发布