Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void BubbleSort(std::vector<int>& vector)
- {
- if (vector.size() >= 2)
- {
- bool sorted = false;
- while (sorted == false)
- {
- static int swaps = 0;
- swaps = 0;
- for (auto i = vector.begin(); i < vector.end() - 1; ++i)
- {
- if (*i > *(i + 1))
- {
- static int cache = *i;
- cache = *i;
- *i = *(i + 1);
- *(i + 1) = cache;
- ++swaps;
- }
- }
- if (swaps == 0)
- {
- sorted = true;
- }
- }
- }
- else
- {
- std::cout << "Vector is too small to bubble sort" << std::endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment