둥지/알고리즘
버블 정렬
by 까닭
2023. 2. 25.
#include <iostream>
#include <vector>
template<typename T>
class DataManager
{
public:
DataManager() {};
DataManager(size_t _Size)
{
Datas.resize(_Size);
};
~DataManager() {};
void Swap(T& _Left, T& _Right)
{
T Temp(_Left);
_Left = _Right;
_Right = Temp;
}
bool Compare(const T& _Left, const T& _Right)
{
return _Left < _Right;
}
void BubbleSort()
{
size_t Size = Datas.size();
for (size_t i = 0; i < Size - 1; ++i)
{
for (size_t j = 0; j < Size - 1; j++)
{
if (true == Compare(Datas[j], Datas[j + 1]))
{
Swap(Datas[j], Datas[j + 1]);
}
}
}
}
size_t Size()
{
return Datas.size();
}
public:
std::vector<T> Datas;
};
int main()
{
DataManager<int> Manager(5);
for (size_t i = 0; i < Manager.Size(); i++)
{
std::cin >> Manager.Datas[i];
}
Manager.BubbleSort();
for (size_t i = 0; i < Manager.Size(); i++)
{
std::cout << Manager.Datas[i];
}
return 0;
}