Skip to content

Instantly share code, notes, and snippets.

@shoooe
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shoooe/9202284 to your computer and use it in GitHub Desktop.
Save shoooe/9202284 to your computer and use it in GitHub Desktop.
Implementation of bubble sort in C++ as an exercise.
#pragma once
#include <algorithm>
template<typename IT>
void bubble_sort(IT begin, IT end) {
while (true) {
bool c = false; // changed?
for (IT i = begin; i != end-1; i = i+1) {
if (*i > *(i+1)) {
std::iter_swap(i, i+1);
c = true;
}
}
if (c == false) return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment