#include "iostream"
using namespace std;
//冒泡排序,外层遍历n-1次,内层每次对n-i-1个数进行比较
template<class T>
void BubbleSort(T arr[],int Length){
for (int i = 0; i < Length - 1; i++)
{
for (int j = 0; j < Length - i - 1; j++)
{
if (arr[j]>arr[j + 1])
{
T temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i <Length; i++)
{
cout << arr[i] << " ";
}
}
int main()
{
int arr[8] = { 9, 4, 6, 2, 1, 5, 8, 2 };
int length;
//表示用arr占用内存大小除以一个int型占用大小
length = sizeof(arr) / sizeof(int);
BubbleSort<int >(arr, length);
//cout << "Hello World!" << endl;//打印hello world
system("pause"); //等待按任意键退出
return 0;
}
运行结果如图: