Created
May 20, 2015 01:29
-
-
Save krysseltillada/c282fa72d2ddbed27694 to your computer and use it in GitHub Desktop.
bubble sort a vector using iterators
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 <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