直接看代码
#include <iostream>
#include <array>
template<typename T, typename Arg>
void Swap(T& T_Container, Arg param1, Arg param2)
{
typename T::value_type Tmp(T_Container[param1]);
T_Container[param1] = T_Container[param2];
T_Container[param2] = Tmp;
}
template<typename T>
void BubbleSort(T& T_Container)
{
auto Flag(true);
for (auto i(0); i < static_cast<decltype(i)>(T_Container.size()) && Flag; ++i)
{
Flag = false;
for (auto j(static_cast<decltype(i)>(T_Container.size()) - 2); j >= i; --j)
{
if (T_Container[j] > T_Container[j + 1])
{
Flag = true;
Swap(T_Container, j, j + 1);
}
}
}
}
template<typename T>
void Display(T& T_Container)
{
std::cout << "排序前:";
for (const auto& i : T_Container)
{
std::cout << i << " ";
}
std::cout << std::endl;
BubbleSort(T_Container);
std::cout << "排序后:";
for (const auto& i : T_Container)
{
std::cout << i << " ";
}
std::cout << std::endl;
}
int main(void)
{
std::array<float, 4> arr{5.3, 1.2, 3.3, 4.4};
Display(arr);
return 0;
}
下面的链接可以查看
https://godbolt.org/z/M6PhYYrWK
有不对的地方请指正=。=