Skip to content

Instantly share code, notes, and snippets.

@y-fedorov
Last active May 22, 2018 08:00
Show Gist options
  • Save y-fedorov/d639c3c5d45a0443c6dfbcc110816b05 to your computer and use it in GitHub Desktop.
Save y-fedorov/d639c3c5d45a0443c6dfbcc110816b05 to your computer and use it in GitHub Desktop.
BubbleSort.cpp
#include <vector>
#include <iostream>
template<class T>
std::vector<T> bubbleSort(std::vector<T> data) {
bool changed;
do {
changed = false;
for (int i = 0; i < data.size() - 1; i++) {
auto &c_value = data[i];
auto &n_value = data[i + 1];
if (c_value < n_value) {
std::swap(c_value, n_value);
changed = true;
}
}
} while (changed);
return data;
}
int main()
{
std::vector<int> dataA = { 5, 1, 4, 2, 8, 6, 7, 8 };
auto resultA = bubbleSort(dataA);
std::vector<double> dataB = { 5.2f, 1.4f, 4.99f, 2, 8, 6, 7, 8 };
auto resultB = bubbleSort(dataB);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment