Last active
May 22, 2018 08:00
-
-
Save y-fedorov/d639c3c5d45a0443c6dfbcc110816b05 to your computer and use it in GitHub Desktop.
BubbleSort.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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