为什么这个方法做不出来正确结果?是哪里出错了?

/*给定一个长度为n的数列,将这个数列按从小到大的顺序排列。1<=n<=200*/
图片说明


#include <iostream>
#include <cmath>
using namespace std;
template<class T>
void bubblesortswap(T& x, T& y)
{
    T temp = x;
    x = y;
    y = temp;
}
template<class T>
void Bubblesort(T* a, int n)
{
    int i = n - 1;
    while (i > 0)
    {
        int lastExchangeIndex = 0;
        for (int j = 0; j < i; j++)
        {
            if (a[j + 1] < a[j])
            {
                bubblesortswap(a[j], a[j + 1]);
                lastExchangeIndex = j;
            }
            i = lastExchangeIndex;
        }
    }
}
int main()
{
    int n=0;
    cin >> n;
    int *a=new int[n];
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    Bubblesort(a, n);
    for (int i = 0; i < n; i++)
    {
        cout << a[i]<<" ";
    }
    cout << endl;
    delete[]a;
    return 0;
}

……你可以带你的数字进去看看结果,就可以知道逻辑那里不完善了。
4和3 ,inedx= 0,1
此时j=0,
交换4 3,
lastExchangeIndex = j; 就是=0
然后
i= lastExchangeIndex; 就是=0
然后就退出while循环了

template
void Bubblesort(T* a, int n)
{
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
{
if (a[j + 1] < a[j])
{
T temp = a[j + 1];
a[j + 1] = a[j];
a[j] = t;
}
}
}
先看这样行不行
如果可以
把交换的代码替换成
bubblesortswap(a[j], a[j + 1]);