Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created May 20, 2015 01:29
Show Gist options
  • Select an option

  • Save krysseltillada/c282fa72d2ddbed27694 to your computer and use it in GitHub Desktop.

Select an option

Save krysseltillada/c282fa72d2ddbed27694 to your computer and use it in GitHub Desktop.
bubble sort a vector using iterators
#include <iostream>
#include <vector>
int main()
{
std::vector<int> num = {22, 1, 3, 4, 100, 4, 1, 6, 7};
auto temp = 0, counter = 0;
for(std::vector<int>::iterator it1 = num.begin(); it1 != num.end(); ++it1) {
for(std::vector<int>::iterator it2 = num.begin() + (counter += 1); it2 != num.end(); ++it2) {
if(*it2 > *it1) {
temp = *it1;
*it1 = *it2;
*it2 = temp;
}
}
}
for(auto it = num.begin(); it != num.end(); ++it)
std::cout << *it << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment