排序算法之冒泡排序

来源:互联网 发布:win7网络共享无法打开 编辑:程序博客网 时间:2024/05/13 02:04
#include <iostream>using namespace std;/***************冒泡排序算法*******************/template<class T>void mySort(T str[],int n){    int i=n-1;    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;}


0 0